Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 1 down vote accepted

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

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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