[英]mod_rewrite trailing slash for directory and remove for file url
好。 我遇到了这个问题,试图删除文件URL中的最后一个斜杠,例如http://domain.com/styles/styles.css/
。 我得到了在末尾添加斜杠的代码,但无法弄清楚如何做条件代码。
如果网址有扩展名,则删除斜杠,否则添加斜杠。
在这里,我现在得到的一些博客说了它的解决方案,但仍无法满足我的期望。
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
还有一个问题,当我输入http://domain.com/index
它会转到http://domain.com/inde/
。 需要您的帮助。。在此先多谢。
在您的htaccess中添加以下代码,以更好地理解。
RewriteCond %{HTTP_HOST} !^\.yourdomain\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
I have a link : domain.com/folder/
it will change to : domain.com//folder
您还可以通过添加DirectorySlash Off来关闭mod_dir的重定向。
为什么要对此类“家具”文件进行外部重定向? 您肯定要在这里进行内部重定向吗?
Options -MultiViews
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)/$ $1 [L]
我建议您如果不使用mutliviews,则应将其关闭,因为这会生成使事情变得混乱的子请求。
您的RewriteCond
条件在逻辑上是相反的,因为您有!
操作员在那里。 因此,重写仅适用于不具有扩展名且不带斜杠的输入!
您可以使用一条没有条件的规则来做到这一点:
# Match any sequence of characters, ending in a dot followed
# by one or more characters that don't contain dots or slashes,
# followed by a final trailing slash.
#
# Rewrite this absolutely and treat as redirect.
RewriteRule ^(.*\.[^./]+)/$ /$1 [L, R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.