I can access my web server as follows: https://www.example.com/my_old_folder/some_folder/
There's an .htaccess file in /my_old_folder/ with the following code:
RewriteEngine on
RewriteRule ^my_old_folder/(.*) my_new_folder/$1
I want to rewrite the folder my_old_folder internally to my_new_folder, without changing the URL in the browser. Just grab the files from /my_new_folder/ instead of /my_old_folder/. If there's another folder like /some_folder/ in this case, keep it. Only change the name /my_old_folder/ to /my_new_folder/.
Unfortunately, it's not rewriting the path, although I already tried many solutions from the internet, including the above one.
Who can help?
Inside /my_old_folder/.htaccess you can use this rule:
RewriteEngine on
RewriteRule .* /my_new_folder/$0 [L]
It is because all path matching is relative to my_old_folder/ inside /my_old_folder/.htaccess.
Related
Maybe someone can point me in the right direction with what I'm trying to do.
I'm trying to re-write so that all subfolders will re-direct to one folder while keeping the first subfolder info, along with the rest of the URL.
Example:
http://www.example.com/billy/profile/info.php
http://www.example.com/mike/profile/info.php
http://www.example.com/sarah/profile/info.php
http://www.example.com/mark/profile/info.php
where it would redirect to:
http://www.example.com/home/profile/info.php?user_name=$1
where I can get the user name and redirect to the correct page.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(?!home/)([^/]+)/(profile/info\.php)$ home/$2?user_name=$1 [L,QSA,NC]
We have a website where we show clients creative work we have produced for them. We upload raw assets to a path like this:
x.com/clients/clientName/campaignName/size/
I have a PHP script which adds our branding, contact information and other information and pulls in the raw creative (usually a swf object). It is in this directory x.com/clients/index.php and it accepts a query string parameter ?path so it knows where to look for the creative.
I am trying to do an apache rewrite in .htaccess so that our designers can upload directly to the known folder structure but so that when you go to x.com/clients/clientName/campaignName/size/ it should rewrite to x.com/clients/index.php?path=clientName/campaignName/size/
I am currently using the following rewrite rule, which works for the first folder level e.g. x.com/clients/clientName/ does successfully rewrite, but any subsequent folders do not.
RewriteRule ^clients/([^/\.]+)/?$ /clients/index.php?path=$1 [L]
My RegEx's are terrible, so I'm stuck on what to do. Any help appreciated, thank you kindly.
Your regex is only matching urls like clients/xxxxxx/ because your pattern [^/\.]+ means one or many characters except "/" or "."
With your rule, it can't work for other subdirectories.
You can change your rule by this one
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
To avoid internal server error (code 500 which means an infinite loop in this case), you can do it this way
RewriteRule ^clients/index\.php$ - [L]
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
Is there a special reason you want to use regex? In my opinion you can just catch everything coming after /clients:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]
RewriteRule ^clients/(.*)$ /clients/index.php?path=$1 [L]
The second line is to prevents redirect loops, because the index.php is also in the folder /clients and this would cause never ending redirects.
I really can't understand how to write htaccess lines. I was trying to Google for few hours and couldn't find anything relevant. Can anybody suggest me a simple .htaccess line that can let me navigate to http://www.mydomain.com/mydirectory/index5.html
by calling it as http://www.mydomain.com/required
I tried in the below manner but I didn't work for me.
RewriteEngine On
RewriteRule ^required mydirectory/index5.html [NC,L]
And, an other question if I place this .htaccess file in the mydirectory folder will that work or am I supposed to place this file only in the root folder?
Thanks.
Put this code in .htaccess in DOCUMENT_ROOT:
RewriteEngine On
RewriteBase /
RewriteRule ^required/?$ /mydirectory/index5.html [NC,L]
PS: You cannot keep this code in /mydirectory since your original URI doesn't contain /mydirectory
Reference: Apache mod_rewrite Introduction
I'm trying to write a rewrite rule to redirect files in my new webhelp system (it's a stand alone website).
I have two things I try to take into account -
I want to redirect only files that are in the http://www.mydomain.com/webhelp folder.
I need to change the .html in the end to .htm...
I manage to find each rule by itself by matching the http://www.mydomain.com/webhelp, but
when I try the rewrite cond it breaks...
Basically, its
http://www.mydomain.com/webhelp/hello.html
into
http://www.mydomain.com/webhelp/hello.htm
without changing
http://www.mydomain.com/index.html
Would really appreciate any help.
Thanks!
If you put the following in a .htaccess file in your webhelp folder, it should achieve what you're looking for:
RewriteEngine on
RewriteBase /webhelp/
RewriteRule ^(.*).html$ $1.htm [R=301,L]
You need to use backreferences. Something like:
RewriteRule ^www/webhelp/(.*)[.]html([#])(.*)[.]html$ www/webhelp/$1.htm$2$3.htm
RewriteRule ^www/webhelp/(.*)[.]html$ www/webhelp/$1.htm
my .htaccess file contains the following
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org\.in [NC]
RewriteRule ^(.*)$ http://mydomain.org.in/$1 [R=301,L]
I moved the whole site to a subfolder and now none of the css and js files in the webpage load. Can anybody tell me what this regex means or why this is happening?
Note: I inherited the site from my seniors :P
It just redirects any request to www.mydomain.org.in/... to mydomain.org.in/...; i.e. it strips the www from the front. However, this shouldn't cause the resource files to break if you simply move it to a subdirectory, assuming you've moved them as well (though you should probably leave the .htaccess file where it is).
It sounds like the links to your CSS/JS files in your HTML might be broken, perhaps because they use absolute URIs (relative to the domain root rather than the current URI). Try checking them first.
As Will explained the .htaccess is not the issue. Your JS and CSS locations were mentioned not relatively and as such when the location of the source files changed they are not being found by the browsers and as such the page is not rendering.
However, you can try the following .htaccess code in addition to the one you are having and see if it links to the files.
RewriteRule ^(.+)\.css$ http://mydomain.org.in/folder/$1.css [R=302,NC]
RewriteRule ^(.+)\.js$ http://mydomain.org.in/folder/$1.js [R=302,NC]
The above code redirects calls to css and js files to a subfolder in your domain. Change folder to the folder you moved everything to.