Mobile app version of vmapp.org
Login or Join
Alves908

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

@Alves908

Posted in: #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).

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Alves908

2 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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/

10% popularity Vote Up Vote Down


 

@Jennifer507

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]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme