: .htaccess Directory Redirect Problem I want to redirect a directory to another directory but I've hit a snag. I want to redirect /directory_name to /new_directory_name so I put this in my .htaccess:
I want to redirect a directory to another directory but I've hit a snag. I want to redirect /directory_name to /new_directory_name so I put this in my .htaccess:
RedirectMatch 301 /directory_name/(.*) /new_directory_name/
Which worked fine BUT I also have another directory called:
/images/directory_name/
And the redirect is redirecting links to files in that directory to /new_directory_name/ and resulting in broken image links.
So I need a redirect that ONLY redirects /directory_name/ and not any other directories that happen to have the same name (but are in a different location).
More posts by @Alves908
2 Comments
Sorted by latest first Latest Oldest Best
RedirectMatch 301 /directory_name/(.*) /new_directory_name/
You simply need to anchor the pattern to the start (^) of the URL, so it matches /directory_name at the start, rather than anywhere in the URL, which is what's happening here.
In other words, prefix the pattern with ^:
RedirectMatch 301 ^/directory_name/(.*) /new_directory_name/
I don't know much about the subject, but I tried the following code on my server and it worked. Check it out.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^a.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.a.com$
RewriteRule ^directory_a/$ "http://a.com/new_directory_a" [R=301,L]
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.