1

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/$1

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).

1

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]
| improve this answer | |
0

RedirectMatch 301 /directory_name/(.*) /new_directory_name/$1

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/$1
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.