Forum Topic: Redirecting for slashes and without slashes

Forum: .htaccess Forum : Redirecting • Posted by Yael Miller • Updated:

Let's say I had a page named pancakes on my site but I've deleted it and want to redirect to my home page. I want to make sure that people who have entered /pancakes/ and /pancakes will be redirected.

Previously, I've done the below:

Redirect 301 /pancakes/ http://example.com
Redirect 301 /pancakes http://example.com

Now I want to have one line instead of two. Which of the below rules is the correct one?

RedirectMatch 301 /pancakes/?$ http://example.com
RedirectMatch 301 /pancakes(.*) http://example.com

2 Replies to “Redirecting for slashes and without slashes”

Jeff Starr
Posted by Jeff Starr

It depends on which case you're after:

/pancakes/?$ will match only the following URLs:

http://example.com/pancakes
http://example.com/pancakes/

On the other hand, /pancakes(.*) matches much more, such as:

http://example.com/pancakes
http://example.com/pancakes/
http://example.com/pancakes/something
http://example.com/pancakes/something/else/
http://example.com/pancakes/anything/else/

So it depends, basically, on whether you're also redirecting any sub-content.

Posted by Yael Miller •

Thanks for explaining.