服务器名称后的两个或多个斜杠 - mod_rewrite

时间:2015-03-02 20:55:06

标签: php apache .htaccess mod-rewrite

我的问题很小: http://server//file

如何删除这两个斜杠?我尝试用这种方法做到这一点:

RewriteCond %{REQUEST_URI} ^/{2,}$ [NC]
RewriteRule . / [R,L]

但它不起作用。

我的.htaccess文件:

RewriteEngine on
RewriteBase /

# hide .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

# remove multi trailing slashes, for simple x/y/ => x/y
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R,L]

# remove more than one slashes when they are in link, for simple x//y => x/y
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R,L]

# rewrite all links server/x to server/x.php
RewriteRule ^([^/]+).* $1.php [L]

2 个答案:

答案 0 :(得分:1)

这应该将两个斜杠(或更多)转换为一个斜杠:


    RewriteCond  %{REQUEST_URI} \ (.*)//+(.*)\  
    RewriteRule .* %1/%2 [R,L]

答案 1 :(得分:1)

您可以使用:

RewriteEngine on
RewriteBase /

# hide .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

# remove multi trailing slashes, for simple x/y/ => x/y
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R,L]

# remove more than one slashes when they are in link, for simple x//y => x/y
RewriteCond %{THE_REQUEST} //
RewriteRule ^(.+?)/?$ /$1 [R,L,NE]

# rewrite all links server/x to server/x.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]