RewriteRule Irrespective of Trailing Slash in .htaccess

rewrite

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/

Best Answer

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

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