htaccess 301 redirect folder, but not subfolders
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]
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.