【问题标题】:Stop WordPress from 301 redirecting /index.php to /阻止 WordPress 从 301 重定向 /index.php 到 /
【发布时间】:2015-06-15 10:22:27
【问题描述】:

我需要能够浏览到 http://www.example.com/index.php,但 WordPress 会自动 301 将其重定向到 http://www.example.com/

是否可以仅为主页停止此重定向?

这是我的 .htaccess 文件:

# 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

【问题讨论】:

  • Wordpress 实际上是在做正确的事。允许使用不同 URI 访问任何内容被视为重复内容。如果是着陆页,那就更糟了。

标签: php wordpress .htaccess http-status-code-301 url-redirection


【解决方案1】:

重定向发生在redirect_canonical 函数中。在重定向发生之前,对重定向 URL 应用了一个过滤器:

$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );

如果您连接到该过滤器,您应该能够禁用重定向。

add_filter('redirect_canonical', function($redirect_url, $requested_url) {
    if($requested_url == home_url('index.php')) {
        return '';
    }
}, 10, 2);

我通过将上述过滤器添加到我的主题的functions.php 文件来验证这是否有效。请注意,必须在重定向操作触发之前附加此过滤器,因此将过滤器放在模板文件中将不起作用。

【讨论】:

  • 我尝试了测试,但它甚至看起来都没有击中我的过滤器。我想知道主页是否没有通过,或者我只是做错了什么。
  • 我通过将该过滤器添加到我的主题的functions.php 文件中验证了它是否有效。你在哪里添加过滤器?
  • 我犯了一个愚蠢的错误。这很好用。谢谢。
【解决方案2】:

应该工作。在RewriteBase / 下添加。

DirectoryIndex <somerandomfile>.php
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://www\.example\.com/index\.php$1 [R=301,L]

【讨论】:

  • 当我尝试使用上面的代码浏览时,我得到了一个重定向循环,它也使 url example.com/index.phpindex.phpindex.php...
  • 你是不是把RewriteBase /下的原代码去掉了?该页面正在循环,因为您的目录索引是“index.php”,所以网页只是重定向到同一页面。您需要更改目录索引才能使其正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
  • 2010-10-27
  • 2014-03-03
  • 2013-02-22
  • 1970-01-01
  • 2012-04-20
相关资源
最近更新 更多