[英]add trailing slash to slug url
我有这样的重写条件,我用它来获取条形网址并搜索这样的博客文章:
http://localhost:8080/blog/my-first-post
因此,我抓住了弹头,然后寻找并显示该帖子。 现在它接受这两种形式
http://localhost:8080/blog/my-first-post
http://localhost:8080/blog/my-first-post/
我应该如何更改.htaccess,以便它始终以末尾的斜杠添加或重定向。
我应该如何更改它以始终删除斜杠?
这是我现在在博客目录中拥有的.htaccess文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
您可以用此代码替换当前的htaccess代码
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
# add trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1/ [R=301,L]
# remove trailing slash
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+?)/$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
现在,它会添加斜杠。 如果要删除它,请注释 add trailing slash
行,并取消注释 remove trailing slash
行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.