在向URI末尾添加“/”时遇到问题

时间:2011-03-21 04:31:00

标签: php .htaccess mod-rewrite rewrite

现在我有一个类似

的网址

http://www.example.com/customer/login

我希望URI总是有一个结束路径,因为恶意使用重定向... 并且如果它没有斜线,那么如果它有一个斜线就可以解决所有问题。我试着在网上查看一些例子,但我真的无法继续工作,这是我当前的.htaccess文件

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?rt=$1 [QSA] 

3 个答案:

答案 0 :(得分:1)

这个怎么样:

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

答案 1 :(得分:1)

Anubhava得到了基本答案,我对他进行了修改。您需要发送HTTP重定向以使浏览器在最后请求带有/的URL。

要与现有的重写规则合并,您应该:

RewriteEngine on

# First check it's not a file
RewriteCond %{REQUEST_FILENAME} !-f
# And it doesn't end in /
RewriteCond %{REQUEST_URI} !.*/$
# Send the redirect. I would do 301 (permanent) here
# the "L" means the rest of the rules are ignored for this request
RewriteRule ^(.*)$ $1/ [L,R=301]

# Now pass thru to your old ruleset URLs the slash-checker didn't catch
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?rt=$1 [QSA]

答案 2 :(得分:0)

尝试使用以下规则

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?rt=$1 [QSA] 

#following rule checks if url is not ending with / and
#if not then redirected to url which is ending with /
RewriteCond %{REQUEST_URI} !.*/$
RewriteRule ^(.*)$  $1/ [R=302,L]
相关问题