【问题标题】:Keeping existing 301 redirects along with wildcard redirection保留现有的 301 重定向和通配符重定向
【发布时间】:2020-01-27 14:24:54
【问题描述】:

我已经手动将一些 URL 从 domainA 重定向到 domainB 像这样

Redirect /abc123 https://domainB.com/abc/?page_url=12

如果任何用户进入 domainA/abc123,他们将登陆 domainB.com/abc/?page_url=12 现在,我想实现这样的通配符重定向

RewriteCond %{HTTP_HOST} !domainB.in$ [NC]
RewriteRule ^(.*)$ https://domainB.in/something/?page_url=1&eid=$1 [L,R=301]

用户可以在 domainA.com/STRING 之后添加任何字符串,并将其传递给 domainB.com/something/?eid=STRING

如何保持现有的手动重定向与通配符重定向不发生冲突?如果可以在 htaccess 中编写 if-else 条件,我该如何实现?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    Redirect 是 mod_alias 指令,RewriteRule 是 mod_rewrite 指令。不同的 Apache 模块在请求期间独立运行并在不同时间运行。 mod_rewrite 将首先运行,尽管配置文件中的指令顺序很明显。您应该避免在同一个配置文件中混合来自两个模块的重定向以避免此类冲突。

    将您现有的 Redirect 指令转换为使用 mod_rewrite 并确保最具体的重定向是第一个。

    例如:

    RewriteEngine On
    
    # Specific redirects
    RewriteRule ^abc123$ https://domainB.com/abc/?page_url=12 [R=302,L]
    
    # General redirect
    RewriteCond %{HTTP_HOST} !domainB\.in [NC]
    RewriteRule (.*) https://domainB.in/something/?page_url=1&eid=$1 [R=301,L]
    

    请注意,您发布的 Redirect 指令未明确说明状态代码,默认为 302(临时)重定向。

    但是,最好使用 302(临时)重定向进行测试以避免缓存问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-04
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2011-07-20
      • 2016-09-07
      相关资源
      最近更新 更多