[英]Remove trailing slash if not a directory with apache
I have the following rewrite rules: 我有以下重写规则:
#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]
I want to add in a rule that removes the trailing slash if someone tries to access site with one. 我想添加一个规则,如果有人试图用一个访问网站,删除尾部斜杠。
eg 例如
website.co.uk/cheese/ should redirect to /cheese website.co.uk/cheese/应该重定向到/ cheese
as you can see I have a rule that redirects ursl with the .php extention, not sure where to begin. 你可以看到我有一个规则,重定向ursl与.php扩展,不知道从哪里开始。
I do have directory in the root folder which I do not wish to remove the trailing url, but I can add a ignore rule for those. 我在根文件夹中有目录,我不想删除尾随URL,但我可以为这些添加一个忽略规则。
Cheers 干杯
Make the change below to your .htaccess file 在下面对.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.