代码之家  ›  专栏  ›  技术社区  ›  ssten

rewriterule不适用于.htaccess中的尾随斜杠

  •  1
  • ssten  · 技术社区  · 7 年前

    我的.htaccess中有一个重写器,它几乎可以按我想要的方式工作。 我想要的是一个规则,它使用可选的尾随斜杠。

    foo.com/bar   > foo.com/index.php?p=bar
    foo.com/bar/  > foo.com/index.php?p=bar
    

    我有的是这个;

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.pdf)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)/?$ index\.php\?p=$1 [NC,L]
    

    但当添加尾随斜杠时,这似乎不起作用。

    1 回复  |  直到 7 年前
        1
  •  1
  •   anubhava    7 年前

    您可以使用此规则允许可选的尾随斜杠:

    RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|bmp|pdf)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+?)/?$ index.php?p=$1 [QSA,L]
    
    推荐文章