【问题标题】:.htaccess file delivering unexpected redirection.htaccess 文件提供意外重定向
【发布时间】:2020-05-17 07:14:45
【问题描述】:

我下面的 .htaccess 代码给出了意想不到的结果。请通过参考预期结果与输出结果来帮助我解决此问题。

<IfModule mod_rewrite.c>
Redirect /main-page https://example.com/new-main-page
Redirect /main-page/sub-page1 https://example.com/new-main-page/new-sub-page1
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

## Added as per WP-1252 ##
<IfModule mod_headers.c>
Header set X-UA-Compatible "IE=edge"
</IfModule>

预期行为:

请求 1:https://sample.com/main-page 重定向到 https://example.com/new-main-page

请求 2:https://sample.com/main-page/sub-page1 重定向到 https://example.com/new-main-page/new-sub-page1

请求 3:https://sample.com/main-page/sub-page2 重定向到 https://sample.com/page-not-found

输出结果:

请求 1:https://sample.com/main-page 重定向到 https://example.com/new-main-page(预期行为)

请求 2:https://sample.com/main-page/sub-page1 重定向到 https://example.com/new-main-page/new-sub-page1(预期行为)

请求 3:https://sample.com/main-page/sub-page2 重定向到 https://example.com/new-main-page/sub-page2(意外行为。为什么?)

【问题讨论】:

    标签: wordpress apache .htaccess mod-rewrite mod-alias


    【解决方案1】:

    这就是Redirect 的工作方式,因为它会捕获以给定 URI 字符串开头的所有内容。所以Redirect /main-page 将重定向所有以/main-page 开头的URI。您应该使用RewriteRule,它允许使用正则表达式进行精确匹配,而且您已经在 .htaccess 的其余部分使用它。

    你可以使用:

    # BEGIN WordPress
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^main-page/?$ https://example.com/new-main-page [L,R=301,NC]
    RewriteRule ^main-page/sub-page1/?$ https://example.com/new-main-page/new-sub-page1 [L,R=301,NC]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    
    ## Added as per WP-1252 ##
    <IfModule mod_headers.c>
    Header set X-UA-Compatible "IE=edge"
    </IfModule>
    

    请务必清除浏览器缓存或使用新浏览器以避免旧浏览器缓存。

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2017-04-15
      • 2012-01-24
      相关资源
      最近更新 更多