apache 删除尾部斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1063894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:49:10  来源:igfitidea点击:

Remove trailing slashes

apache.htaccessmod-rewrite

提问by Erik

I'd like to work with pages without trailing slashes. So now I want my URL's with an trailing slash to redirect (using .htaccess) to the same URL without the trailing slash.

我想处理没有尾部斜杠的页面。所以现在我希望我的 URL 带有尾部斜杠重定向(使用 .htaccess)到没有尾部斜杠的相同 URL。

I got two .htaccess files:

我有两个 .htaccess 文件:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteRule (.*)  public/
</IfModule>

And one in my public folder:

还有一个在我的公共文件夹中:

DirectoryIndex index.html index.php
Options -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On        
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f         
    RewriteRule ^(.*)$ index.php?url= [PT,L]
</IfModule>

I tried adding the following rule to the .htaccess file in the public folder:

我尝试将以下规则添加到公用文件夹中的 .htaccess 文件中:

RewriteRule (.*)/$  [R,L]

But then: example.com/public/page/view/2/

但随后:example.com/public/page/view/2/

Redirects to: example.com/**D:/webserver/**public/page/view/2

重定向到:example.com/**D:/webserver/**public/page/view/2

Which is obviously not what I want...

这显然不是我想要的......

采纳答案by Fake51

Why do you have several .htaccess files? Where are the one's redirecting to public/$1 stored? You may very well be facing overriding RewriteRule directives that complicate things for you.

为什么你有几个 .htaccess 文件?重定向到 public/$1 的存储在哪里?您很可能会面临重写的 RewriteRule 指令,这使您的事情变得复杂。

Without knowing your setup it's fairly hard to say how you should be using rewrites - can you specify what things are ACTUALLY looking like?

在不知道您的设置的情况下,很难说您应该如何使用重写 - 您能指定实际情况吗?

回答by swedish boy

Wordpress redirects depending on what your permalink structure is. So any changes to .htaccess helps little since Wordpress will add/remove them for you and peform a wp_redirect() after .htaccess done things. In worst case you end up with a redirect loop.

Wordpress 重定向取决于您的永久链接结构。因此,对 .htaccess 的任何更改都无济于事,因为 Wordpress 会为您添加/删除它们,并在 .htaccess 完成后执行 wp_redirect()。在最坏的情况下,您最终会出现重定向循环。

A solution is to turn off Wordpress redirecting using.

一个解决方案是使用关闭 Wordpress 重定向。

// perhaps an if(is_something()) before here ...
add_filter('redirect_canonical', '__return_false');

You should wrap this inside an if statement or such where you perform a check for a certain page/directory/etc. Putting the above directly in your functions file will turn off wordpress redirections and probably break things.

你应该把它包装在一个 if 语句中,或者在你对某个页面/目录/等执行检查的地方。将上述内容直接放在您的函数文件中将关闭 wordpress 重定向并可能破坏某些内容。

回答by Ion Br.

Here is a an example from Apache documentation which you can use to solve your issue:

这是 Apache 文档中的一个示例,您可以使用它来解决您的问题:

Trailing Slash Problem

尾随斜线问题

Description:

描述:

Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. And because this file is a directory it complains. Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc.

每个网站管理员都可以唱一首关于 URL 引用目录的尾部斜杠问题的歌曲。如果它们丢失,服务器会转储一个错误,因为如果你说 /~quux/foo 而不是 /~quux/foo/ 那么服务器会搜索一个名为 foo 的文件。因为这个文件是一个目录,所以它会抱怨。实际上,在大多数情况下它会尝试自行修复,但有时这种机制需要您模仿。例如,在您对 CGI 脚本等进行了大量复杂的 URL 重写之后。

Solution:

解决方案:

The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect!

这个微妙问题的解决方案是让服务器自动添加尾部斜杠。要正确执行此操作,我们必须使用外部重定向,以便浏览器正确请求后续图像等。如果我们仅进行内部重写,则这仅适用于目录页面,但是当任何图像包含在此页面中时会出错使用相对 URL,因为浏览器会请求一个内联对象。例如,如果没有外部重定向,对 /~quux/foo/index.html 中的 image.gif 的请求将变成 /~quux/image.gif!

So, to do this trick we write:

所以,要做到这一点,我们写:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]

The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. But notice that this creates some processing overhead.

疯狂和懒惰的人甚至可以在他们的 homedir 的顶级 .htaccess 文件中执行以下操作。但请注意,这会产生一些处理开销。

RewriteEngine  on
RewriteBase    /~quux/
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$ / [R]

Source

来源

回答by Daniel James

Have you tried adding:

您是否尝试添加:

RewriteBase /public

to the .htaccessfile in the public folder, to get:

.htaccess公共文件夹中的文件,以获取:

<IfModule mod_rewrite.c>
    RewriteEngine On            
    RewriteBase /public

    RewriteRule (.*)/$  [R,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f         
    RewriteRule ^(.*)$ index.php?url= [PT,L]
</IfModule>