1

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

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

Now problem is with URL structure like this:

http://www.domain.com/old-url-1/sub-url/ or http://www.domain.com/old-url-2/sub-url/ which is not getting to desired path on redirected url to http://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: http://www.domain.com/old-url-1/ redirect visitor to http://www.domain.com/new-url-3/, but if someone goes to http://www.domain.com/old-url-1/sub-url/keep visitor on that URL, and don't redirect to http://www.domain.com/new-url-3/sub-url.

  • 1
    You made the question more complicated than the answer would be :_) – Simon Hayter Sep 27 '15 at 19:35
  • I agree with that :( That's why I added EDIT in bold. – Alan Kis Sep 27 '15 at 19:36
2

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/ http://www.domain.com/new-url-3/

Becames

RedirectMatch 301 ^/old-url-1/$ http://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.

| improve this answer | |
  • 1
    Because... Redirect is prefix matching (whole path segments) and appends everything after the match onto the destination URL. Whereas RedirectMatch is not, matching the regular expression instead. – MrWhite Sep 28 '15 at 8:34

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.