【问题标题】:htaccess rewrite when query string is empty does not works查询字符串为空时的 htaccess 重写不起作用
【发布时间】:2019-02-07 02:42:40
【问题描述】:

我正在尝试进行从 mysite.commysite.com/root/ 的 302 重定向,以避免将 mysite.com/?s=hello 重定向到 mysite.com/root/?s=hello

目前我正在使用此代码:

RewriteEngine On

# Redirect Home
RewriteCond %{QUERY_STRING} ^$
RedirectMatch ^/$ /root/

# WordPress Redirect
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

这不起作用,因为在这两种情况下都会重定向。

【问题讨论】:

    标签: wordpress .htaccess redirect


    【解决方案1】:

    RedirectMatch 不适合 RewriteCond。它应该是RewriteRule 并且还需要修复正则表达式模式:

    RewriteEngine On
    RewriteBase /
    
    # Redirect Home
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^/?$ /root/ [L,R=302]
    
    # WordPress Redirect
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    

    注意在第一条规则中使用R=302 以获得302 状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-26
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      相关资源
      最近更新 更多