Mobile app version of vmapp.org
Login or Join
Megan663

: How to redirect the root of a directory in .htaccess without also redirecting all the contents? I have two specific URL which are being redirected with 301 redirect in my .htaccess file like

@Megan663

Posted in: #Directory #Folder #Htaccess #Redirects #Url

I have two specific URL which are being redirected with 301 redirect in my .htaccess file like below:

redirect 301 /old-url-1/ www.domain.com/new-url-3/ redirect 301 /old-url-2/ www.domain.com/new-url-3/

Now problem is with URL structure like this:
www.domain.com/old-url-1/sub-url/ or www.domain.com/old-url-2/sub-url/ which is not getting to desired path on redirected url to www.domain.com/old-url-1/sub-url/.
How can I only match /old-url-1/ part, and redirect in that case, in all other cases just follow original path like /old-url-1/sub-url/?

**Desired effect is like this:
If someone goes to: www.domain.com/old-url-1/ redirect visitor to www.domain.com/new-url-3/, but if someone goes to www.domain.com/old-url-1/sub-url/keep visitor on that URL, and don't redirect to www.domain.com/new-url-3/sub-url.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Megan663

1 Comments

Sorted by latest first Latest Oldest Best

 

@Megan663

The key to redirect only exact URL match is to tell redirectMatch to match only exactly URL which is easy achieved with regex according to httpd.apache.org, where description says:


Description: Sends an external redirect based on a regular expression match of the current URL
Syntax: RedirectMatch [status] regex URL


In my case,

redirect 301 /old-url-1/ www.domain.com/new-url-3/

Becames

RedirectMatch 301 ^/old-url-1/$ www.domain.com/new-url-3/

Where ^matches the beginning of URL, and $ to match the end of URL.

So my rule only will match old-url-1, and not old-url-1/sub-url or some another odl-url-1/some-sub-path.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme