Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 793
Shane Avatar asked Sep 28 '22 08:09

Shane


1 Answers

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.

like image 188
Mathew Tinsley Avatar answered Oct 03 '22 02:10

Mathew Tinsley