使用htaccess为specefic路由删除尾部斜杠

时间:2017-06-06 16:45:49

标签: .htaccess mod-rewrite

我使用.htaccess在网址末尾添加了一个尾部斜杠。 有没有办法排除特定路线添加该尾随斜杠

现在发生了什么:

example.com/sitemap/

我想要的是什么:

example.com/sitemap

这是我的.htaccess文件:

</IfModule>

        RewriteEngine on
        RewriteCond %{HTTPS} !on
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

        RewriteCond %{HTTP_HOST} !^$
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteCond %{HTTPS}s ^on(s)|
        RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]

1 个答案:

答案 0 :(得分:0)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]

这似乎是附加尾部斜杠的代码。因此,您可以在排除上述网址中添加其他条件。例如:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/sitemap$
RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]

CondPattern 上的!前缀否定了正则表达式。因此,!^/sitemap$之类的模式将匹配任何非/sitemap的网址。

在测试之前,您需要清除浏览器缓存。