How to redirect with AND without trailing slashes

Topics: User Forum
Jan 11, 2013 at 5:06 PM

Hello,

Could someone please help me with how do I redirect to the same page regardless whether the URL has trailing slash or not?

Example:

I would like both "www.mysite.com/alien" AND "www.mysite.com/alien/" (notice the trailing slash) to redirect to the SAME page.

The trick is, first of all, that I have quite a few URLs that requires such redirects.

And second, there are URLs that are similar so the redirect needs to be able to distinguish between them. What I mean is, I had the problem, where I had "www.mysite.com/alien" redirect to one page, and "www.mysite.com/alienated" redirect to a DIFFERENT page. Since part of the word "alien" can be found in both URLs, somehow the redirect found the first entry "www.mysite.com/alien" and immediately redirected there, without checking that I actually typed in "/alienated" so it should've redirected to that page. But instead, it just immediately found "/alien" and redirected there, skipping the other URL (where I wanted to go).

Currently I have the following redirect rules setup for each URL:

RedirectRule ^/alien$ http://www.mysite.com/concepts/foreign/aliens.htm   [R=301]

With this, I seem to get different results with or without trailing slashes after "alien".

 

Thank you!

Jan 14, 2013 at 9:36 AM

If you make sure that your rules are in the right order , you should overcome the case where /alienated isn't picked up. I.e. make sure that your /alienated redirect is listed higher in your rule set.

To pick up on a url with and without a forward slash, just add /? to the end of your regular expression.

RedirectRule ^/alienated/?$ http://www.mysite.com/concepts/foreign/alienated.htm   [R=301]
RedirectRule ^/alien/?$ http://www.mysite.com/concepts/foreign/aliens.htm   [R=301]

The question mark means "0 or more", so will match whether it's there or not.

HTH

Jan 19, 2013 at 1:09 AM

Thanks for the reply shonk!

I added ^/alienated/*$ before I saw your post, which seemed to work as well.

Jan 21, 2013 at 9:31 AM

The rule as you've shown will always need to have a forward slash at the end, so consider the question mark, which makes it optional.

Your rule breaks down as follows:

Any string starting with a forward slash, followed by the word 'alienated', followed by a forward slash, followed by one single character only. I'm not sure that this will match what you're after effectively. I hope it does.

Cheers.