简体   繁体   English

尾部斜杠删除重定向在.htaccess apache中不起作用

[英]trailing slash removal redirection not working in .htaccess apache

I am working on a project in which i have migrated old to new site. 我正在做一个项目,其中我已将旧的迁移到新的站点。 SEO is still working for old site so i have written rules in my yii2 application as SEO仍在旧站点上工作,因此我在yii2应用程序中编写了规则

'rules' => [
                "<year:\d{4}>/<number1:\d{2}>/<number2:\d{2}>/<slug>"=>'video/parse',

This yii2 redirection is working fine for URLS not ending in trailing slash. 对于没有以斜杠结尾的URL,此yii2重定向工作正常。 But this rule fails for trailing slash URLs. 但是此规则对于尾部斜杠URL无效。 To cope with this problem I tried to redirect through apache. 为了解决这个问题,我尝试通过apache重定向。 To do so i write this code 为此,我编写此代码

<VirtualHost *:80>
RewriteEngine on

RewriteCond %{SERVER_NAME} =mydomain.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

Problem1: 问题1:
When I hit http://example.org/2017/09/10/slug/ then it is redirected to http://example.org//2017/09/10/slug/ as you can see an additional slash is added after domain name. 当我点击http://example.org/2017/09/10/slug/时,它会重定向到http://example.org//2017/09/10/slug/ ,您会看到添加了另一个斜杠域名之后。
Problem2: Problem2:
When I hit https://example.org/2017/09/10/slug/ then it is not redirected anyway. 当我点击https://example.org/2017/09/10/slug/时,它还是不会重定向。

Questions: 问题:
1. How do we redirect for trailing slash? 1.我们如何重定向斜杠?
2. How to get the redirection working for https? 2.如何使重定向适用于https?

You should probably use UrlNormalizer for such redirections: 您可能应该使用UrlNormalizer进行此类重定向:

'urlManager' => [
    // ...
    'normalizer' => [
        'class' => yii\web\UrlNormalizer::class,
        // you can use temporary redirection instead of permanent for tests
        // 'action' => UrlNormalizer::ACTION_REDIRECT_TEMPORARY,
    ],
],

You can read more about URL normalization in this guide article . 您可以在本指南文章中阅读有关URL规范化的更多信息。

The issue you face is that you are actually not using a dynamic configuration file (".htaccess") as you claim (I assume that, since the VirtualHost directive is invalid in there). 您面临的问题是您实际上没有按照声明使用动态配置文件(“ .htaccess”)(我认为是这样,因为其中的VirtualHost指令无效)。 And because the RewriteRule logic works on absolute paths when used in the real http server configuration files as opposed to relative paths when used in dynamic configuration files. 并且因为RewriteRule逻辑在实际的http服务器配置文件中使用时在绝对路径上工作,而在动态配置文件中使用时则在相对路径上工作。 This is documented pretty prominently, actually. 实际上,这是非常明显的记录。 That difference in logic results in your capture (which you reuse with the $1 reference) to contain an actual leading slash. 逻辑上的差异导致捕获(您可以将其与$1引用重用)包含实际的前斜杠。

So all you need to do is to change is your rule to this more robust version: 因此,您要做的就是更改规则,以使用更强大的版本:

RewriteRule ^/?(.*)/$ /$1 [R=301,L]

That leading ^/? 那领先的^/? allows an unmodified rule to be used likewise in both versions of configuration files with identical logic. 允许在逻辑相同的两个版本的配置文件中同样使用未修改的规则。 The reason for that should be clear, I assume. 我认为这样做的原因应该很清楚。

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. 一个好主意是从302临时重定向开始,然后在确定一切都已正确设置之后,才将其更改为301永久重定向。 That prevents caching issues while trying things out... 这样可以防止在尝试时缓存问题...

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). 还有一个一般性说明:您应该始终喜欢将此类规则放在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。 Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. 这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。 They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare). 仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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