如果不是带有apache的目录,请删除尾部斜杠

时间:2012-01-05 15:14:32

标签: apache .htaccess mod-rewrite trailing-slash

我有以下重写规则:

#remove the www.
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ http://local.website.co.uk/$1 [R=301,L]

#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]

如果有人试图访问带有斜杠的网站,我想添加一条删除尾部斜杠的规则。

例如

website.co.uk/cheese/应该重定向到/ cheese

你可以看到我有一个规则,用.php扩展重定向ursl,不知道从哪里开始。

我在根文件夹中有目录,我不想删除跟踪网址,但我可以为这些添加一个忽略规则。

干杯

1 个答案:

答案 0 :(得分:14)

将以下更改更改为.htaccess文件

RewriteEngine on
RewriteBase /

#existing rule
#remove the www.
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ http://local.website.co.uk/$1 [R=301,L]

#new Rule
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]

# rest of your existing rules go here
相关问题