2012-01-05 45 views
5

我有以下重寫規則:刪除斜線如果沒有目錄與Apache

#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/應該重定向到/奶酪

,你可以看到我有一個重定向輸尿管鏡氣壓彈道碎石和.php extention的規則,不知道從哪裏開始。

我確實在根文件夾中有一個目錄,我不想刪除尾部URL,但我可以爲這些目錄添加一個忽略規則。

乾杯

回答

13

讓下面.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 
+0

完美的感謝! – AJFMEDIA 2012-01-05 15:36:35