如果网址包含关键字,则添加尾部斜杠



我已经尝试了几个版本的尾随斜杠代码,但没有一个对我有用。我希望仅在网址包含关键字新闻时才添加尾部斜杠,因此

domain.com/news
domain.com/news/category/foo
domain.com/news/archive/august-2018

应该触发它添加斜杠。我现在有这个

RewriteCond %{REQUEST_URI} ^/(news.*)$
RewriteRule ^/news(.*[^/])$ %{REQUEST_URI}/ [R=301,L]

尝试使用以下规则,

RewriteRule ^news$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301]

您可以使用两个单独的规则来执行此操作,一个用于单独news,另一个用于news/categroy/foo

# direct news to news/
RewriteRule ^news$ news/ [L,R=301]
# if no trailing slash, direct news/* to news/*/
RewriteCond %{REQUEST_URI} ^(.*[^/])$
RewriteRule ^news/(.*)$ news/$1/ [L,R=301]

这三条规则导致:

http://www.example.com/news => http://www.example.com/news/
http://www.example.com/news/category/foo => http://www.example.com/news/category/foo/
http://www.example.com/news/archive/august-2018 => http://www.example.com/news/archive/august-2018/

您可以使用 htaccess.madewithlove.be 测试这些规则。

最新更新