I need to redirect from:
http://example.com/folder
to http://example.com/newfolder
But leave:
http://example.com/folder/subfolder
Where it is. Is this possible? I can't seem to make it happen without causing a heap of redirect chaos.
Jestep's answer above redirects "/root-directory" but fails to redirect "/root-directory/". This can be fixed and simplified by simply:
RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteRule (.*) /newfolder [R=301,L]
This will redirect "/folder" and "/folder/" but leave all sub-directories alone.
I've tried other methods here with mixed success. I am no Apache expert by any means, but here is the simple one-liner that works every time for me. I just use RedirectMatch instead of Redirect and include a very simple RegEx to end the match at or just before the trailing slash, meaning that subdirectories should never qualify as a match.
RedirectMatch 301 ^/folder[/]?$ /newfolder
I just ran into this and here's the solution I came up with. I prefer this method because it doesn't redirect any subdirectory.
RewriteCond %{REQUEST_URI} ^/root-directory[/]?
RewriteCond %{REQUEST_URI} !^/root-directory/+[/]?
RewriteRule (.*) http://www.example.com/ [R=301,L]
Maybe:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder[/]?
RewriteCond %{REQUEST_URI} !^/folder/subfolder[/]?
RewriteRule (.*) /newfolder/$1 [R=301,L]
This should redirect /folder to /newfolder but leave out /folder/subfolder
If mod-rewrite isn't enabled or you are unable to use RewriteRule directive on your server, you can use RedirectMatch directive of mod-alias which is the default module of apache httpd server.
RedirectMatch 301 ^/folder/?$ http://example.com/newfolder/
This will 301 redirect /folder/ to /newfolder/ .
Which server are you using?
You could for example use mod_rewrite if you use apache and do something like this
RewriteEngine On
RewriteOptions Inherit
RedirectMatch permanent ^/folder/$ http://example.com/newfolder
#I haven't tested the above redirect btw ^
and put that in a .htaccess file in your /folder/ directory (assuming you can alter apache's settings, meaning you have the option AllowOverride All in that virtual host)
Here's some more info http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Related
I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]
I have many URLs like this:
https://www.url.com/user/shop/location/scoobydoo
They all begin the same, but the username at the end is different. I am trying to redirect 301 them all to:
https://www.url.com/profile-scoobydoo
This works if I use:
RewriteCond %{HTTP_HOST} www\.url\.com$
RewriteCond %{REQUEST_URI} ^\/user\/shop\/location\/scoobydoo$
RewriteRule .* https://www.url.com/profile-scoobydoo [R=301,L]
The problem is, I have several thousand users and while generating them all would work, it would completely flood the .htaccess file. Surely there must be an easier way to do this by username?
But how? Any help is greatly appreciated.
Try :
RewriteEngine on
RewriteRule ^user/shop/location/(.+)$ https://example.com/profile-$1 [L,R=301]
You can also use RedirectMatch directive
for your url redirection
RedirectMatch 301 ^/user/shop/location/(.+)$ https://example.com/profile-$1
I am needing to forward a whole domain minus one folder and it's contents.
http://www.domain.com/folder_I_need/content_I_need.stuff
The rest of the site needs to forward.
Here is what I've tried:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder_I_need/$
Redirect 301 / http://www.new_domain.org/
The RewriteCond directive is part of mod_rewrite and Redirect directive part of mod_alias, they don't work in conjunction with each other. You also probably don't want the $ at the end of your condition's pattern. Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder_I_need/
RewriteRule ^(.*)$ http://www.new_domain.org/$1 [L,R=301]
I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]
I want to redirect http://site.com/home?page=123 http://site.com/home
but the following rule doesnt work
redirectMatch 301 ^/home/\?(.*)$ http://www.site.com/
Any help would be appreciated. Thanks
Unfortunately RedirectMatch directive does not work with query string -- only with path part of the URL. You have to use mod_rewrite for that:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =page=123
RewriteRule ^home$ http://www.site.com/? [R=301,L]
Place it in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
It will ONLY redirect request for /home?page=123. All other requests (e.g. /home?page=123&extra=hello) will be ignored.