删除斜杠如果没有与Apache目录 [英] Remove trailing slash if not a directory with apache

查看:242
本文介绍了删除斜杠如果没有与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]

我想在删除了结尾的斜线规则添加,如果有人试图访问一个网站。

I want to add in a rule that removes the trailing slash if someone tries to access site with one.

例如

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

website.co.uk/cheese/ should redirect to /cheese

你可以看到我有一个重定向输尿管镜气压弹道碎石和.php延伸的规则,不知道从哪里开始。

as you can see I have a rule that redirects ursl with the .php extention, not sure where to begin.

我确实有我不希望删除尾随URL的根文件夹目录,但我可以添加一个无视规则的。

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.

干杯

推荐答案

请在下面给你的.htaccess文件的变化

Make the change below to your .htaccess file

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

这篇关于删除斜杠如果没有与Apache目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆