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?

Read More

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

Related posts

Leave a Reply

2 comments

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

  2. 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]