简体   繁体   English

mod_rewrite用于目录的结尾斜杠,用于文件url的删除

[英]mod_rewrite trailing slash for directory and remove for file url

Ok. 好。 I got this problem I trying to remove the last slash in a file url for example http://domain.com/styles/styles.css/ . 我遇到了这个问题,试图删除文件URL中的最后一个斜杠,例如http://domain.com/styles/styles.css/ I got the code for adding slash to the end but cannot figure how to do the conditional. 我得到了在末尾添加斜杠的代码,但无法弄清楚如何做条件代码。

If the URL has an extesion then remove end slash else add slash.. 如果网址有扩展名,则删除斜杠,否则添加斜杠。

Here what I got right now some blogs says its the solution but still isn't working for what I expect. 在这里,我现在得到的一些博客说了它的解决方案,但仍无法满足我的期望。

RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

Also a problem, when I type http://domain.com/index it goes to http://domain.com/inde/ . 还有一个问题,当我输入http://domain.com/index它会转到http://domain.com/inde/ Need your help guys.. Thanks a lot in advance. 需要您的帮助。。在此先多谢。

add following code in your htaccess, for better understanding. 在您的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

also You can turn off mod_dir's redirect by including a DirectorySlash Off. 您还可以通过添加DirectorySlash Off来关闭mod_dir的重定向。

Why do you want to do an external redirect for such "furniture" files? 为什么要对此类“家具”文件进行外部重定向? Surely an internal redirect is what you want here? 您肯定要在这里进行内部重定向吗?

Options     -MultiViews

RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)/$        $1         [L]

I advise that you turn off mutliviews if you don't use it as this can generate subrequests which confuse things. 我建议您如果不使用mutliviews,则应将其关闭,因为这会生成使事情变得混乱的子请求。

Your RewriteCond conditions are logically inverted because you have the ! 您的RewriteCond条件在逻辑上是相反的,因为您有! operator there. 操作员在那里。 So the rewrite is applying only for those inputs which do not have extensions, and which do not have a trailing slash! 因此,重写仅适用于不具有扩展名且不带斜杠的输入!

You can do this with a single rule with no conditions: 您可以使用一条没有条件的规则来做到这一点:

# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM