简体   繁体   中英

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/ .

Is it possible to stop this redirection ONLY for the homepage?

Here is my .htaccess file:

# 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. There is a filter applied to the redirect URL before the redirection occurs:

$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. 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 / .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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