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

Stop WordPress from 301 redirecting /index.php to /

我需要能够浏览到 http://www.example.com/index.php, but WordPress automatically 301 redirects this to 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

重定向发生在 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 文件中验证了它是否有效。请注意,必须在重定向操作触发之前附加此过滤器,因此将过滤器放在模板文件中将不起作用。

这个应该有效。在 RewriteBase / 下添加。

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