0

I want to redirect URLs irrespective of trailing slashes. The reason is because my site enforces trailing slashes by default; if an internal link is missing the trailing slash then I have a redirect chain ( /test -> /test/ -> /go-here/).

I've tried:

  • This does not work: RewriteRule ^test(/$|$) /go-here/ [R=301,L]
  • This works: Redirect 301 /test /go-here/ (but I don't want to do this)

Essentially, I want a good way to redirect URLs for an instance like that below without having to have tons of lines in my .htacess file.

  • /test -> /go-here/
  • /test/ -> /go-here/

2 Answers 2

0

Using the "once of nonce" operator which is short for {0,1}

RewriteRule ^test/?$ /go-here/ [R=301,L]
0

As mentioned above you can use the ? operator to match it with a slash and without, this is the most precise way.

Another way worth mentioning though is to just match on the start of the string. ^ matches the start and $ matches the end of a string in a regular expression. Usually people include both of those, but you don't have to include them both or even either of them (if you want to match on a string in the middle that isn't near the start or end).

So, this will also accomplish what you are after:

RewriteRule ^test /go-here/ [L,R=301]

Anything starting with "test" will get matched, regardless of slash or anything else that comes afterwards.

Be careful though as this will also match /testing or /tester.

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.