URL重写在结尾处删除斜杠

时间:2013-08-06 08:10:11

标签: rewrite

我有这个工作代码,

RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^(.*)\/$ /page.php?page=$1 [L]
RewriteRule ^news/(.*)\/$ /news/article.php?id=$1 [L]

我要删除URL末尾的/;这会产生类似的结果(见最后两行的变化):

RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^(.*)\$ /page.php?page=$1 [L]
RewriteRule ^news/(.*)\$ /news/article.php?id=$1 [L]

但是,如果我在尝试访问该文件时将其保留,例如www.domain.com/register而不是www.domain.com/register/,则会抛出错误404.

1 个答案:

答案 0 :(得分:0)

您正在寻找这个简单的陈述:

(\/{0,1})

它说“可以有一个/或零/ - 两者都很好”。当应用最后两行时,整个结果应如下所示:

RewriteRule ^(.*)\$ /page.php?page=$1(\/{0,1}) [L]
RewriteRule ^news/(.*)\$ /news/article.php?id=$1 [L]

如果它仍然无法正常工作,请尝试将(\/{0,1})添加到最后一行的末尾。