.htaccess regular expression need to make trailing forward slash optional

后端 未结 1 691
陌清茗
陌清茗 2021-01-05 08:43

I need to include an optional trailing forwardslash, that\'s an /, in my RewriteRule

What I have so far is

RewriteRule ^([a-zA-Z0-9]+)$ u.php?$1|$2
<         


        
相关标签:
1条回答
  • 2021-01-05 09:24

    Just put /? before the $ at the end in your pattern:

    RewriteRule ^([a-zA-Z0-9]+)/?$ u.php?$1
    

    But I would rather suggest you to allow just one spelling (either with or without trailing slash) and redirect the other one:

    # remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/$ /$1 [L,R=301]
    # add trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .*[^/]$ /$0/ [L,R=301]
    
    0 讨论(0)
提交回复
热议问题