Apache rewrite rule which works with or without a trailing slash

后端 未结 4 1738
南旧
南旧 2021-02-04 01:27

I\'m trying to redirect a series of static URLs, and I want it to work whether or not the trailing slash is present:

/foo/bar  --->  /tacos
/foo/bar/  -->          


        
相关标签:
4条回答
  • 2021-02-04 01:43

    Try

    RewriteRule ^foo/bar/?$ http://url.com/tacos
    
    0 讨论(0)
  • 2021-02-04 01:49

    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
    
    0 讨论(0)
  • 2021-02-04 01:51

    This also works: RedirectMatch 301 /foo/bar(/.*|$) http://url.com/tacos

    0 讨论(0)
  • 2021-02-04 02:08

    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
    
    0 讨论(0)
提交回复
热议问题