简体   繁体   English

停止WordPress从301重定向/index.php到/

[英]Stop WordPress from 301 redirecting /index.php to /

I need to be able to browse to http://www.example.com/index.php , but WordPress automatically 301 redirects this to http://www.example.com/ . 我需要能够浏览到http://www.example.com/index.php ,但WordPress会自动301将其重定向到http://www.example.com/

Is it possible to stop this redirection ONLY for the homepage? 是否可以仅为主页停止此重定向?

Here is my .htaccess file: 这是我的.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

The redirection occurs in the redirect_canonical function. 重定向发生在redirect_canonical函数中。 There is a filter applied to the redirect URL before the redirection occurs: 在重定向发生之前,有一个过滤器应用于重定向URL:

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

If you hook into that filter you should be able to disable the redirection. 如果您挂钩到该过滤器,您应该能够禁用重定向。

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

I verified that this is working by adding the above filter to my theme's functions.php file. 我通过将上面的过滤器添加到我的主题的functions.php文件中来验证这是有效的。 Note that this filter must be attached before the redirect action fires so placing the filter in a template file will not work. 请注意,必须在重定向操作触发之前附加此过滤器,因此将过滤器放在模板文件中将不起作用。

This should work. 应该工作。 Add under RewriteBase / . RewriteBase /下添加。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM