【问题标题】:Redirects aren't working, is there a conflict with the rules or order of them?重定向不起作用,是否与它们的规则或顺序发生冲突?
【发布时间】:2019-11-30 20:53:18
【问题描述】:

我需要将任何不是 www 或 https 的传入流量重定向到 www 和 https。 一个问题是尝试再添加一条规则来处理曾经位于 rootdomain.com/blog/rest-of-url-title 的所有流量并将其发送到子域 https://blog.rootdomain.com/rest-of-url-title

这是我的根域 htaccess 中的内容

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/blog/(.*)$ https://blog.rootdomain.com/$1 [R=302,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=302]
</IfModule>

然后在博客子域目录 htaccess 中我有这个(将任何非 https 重定向到 https,其余的是基于 Wordpress)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

当尝试访问任何 URL 是 rootdomain.com/blog/rest-of-url-title 时,它​​会重定向到 https://www.rootdomain.com/index.php

它没有重定向到博客子域,而是转到 www,由于 www 不存在该 url 标题,我只得到 404。

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    我必须使用 mod_alias 重定向匹配才能让它按我想要的方式工作

    RedirectMatch 302 ^/blog/(.*)$ https://blog.rootdomain.com/$1
    

    这很奇怪,因为我在 root 中的原始 htaccess 中的 mod_rewrite 行有这个:

    RewriteRule ^blog/(.*) https://blog.rootdomain.com/$1 [R=302,L]
    

    这在这个特定的服务器上不起作用,但在我测试过的任何其他服务器上都可以正常工作。可能是因为子域/目录映射问题?

    无论如何,我知道混合 mod_rewrite 和 mod_alias 不是一个好习惯,这个最终对我有用:

    RedirectMatch 302 ^/blog/(.*)$ https://blog.skodaminotti.com/$1
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=302]
    </IfModule>
    

    如果有人知道为什么重写不起作用,我仍然很想听听。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2017-02-20
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多