Stop WordPress from 301 redirecting /index.php to / Stop WordPress from 301 redirecting /index.php to / wordpress wordpress

Stop WordPress from 301 redirecting /index.php to /


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>.phpRewriteCond %{HTTP_HOST} ^www\.example\.com [NC]RewriteRule (.*) http://www\.example\.com/index\.php$1 [R=301,L]