Apache rewrite rule which works with or without a trailing slash

If you want to match foo/bar regardless of whether it's followed by another portion of path, you can say:

RewriteRule ^foo/bar(/.*|$) http://url.com/tacos

This will match any of the following:

foo/bar
foo/bar/
foo/bar/baz

It means: match either a) a slash followed by 0 or more characters, or b) the end of the string.

On the other hand, these might be undesirable:

RewriteRule ^foo/bar/? http://url.com/tacos     # This also matches foo/barb
RewriteRule ^foo/bar/?$ http://url.com/tacos    # This will not match foo/bar/baz

Other than in EBNF or ABNF, a quantifier in regular expressions refers the preceding expression and not the following expression.

So:

RewriteRule ^foo/bar/?$ http://url.com/tacos

Try

RewriteRule ^foo/bar/?$ http://url.com/tacos

Tags:

Url

Regex

Rewrite