The Blog

WordPress index.php Redirects to Base Url

Problem

Recently, I was working on a website that needed to display a temporary home page, while an installation of WordPress was being worked on. The custom home page was being displayed as index.html. WordPress uses index.php, combined with mod_rewrite, to handle all requests coming into the site.

The server I was using would show index.html by default. So, if someone visited my site by typing in the primary url (e.g. http://www.myexamplesite.com/), they would see the index.html file by default.

My plan was to leave the index.html file in place and install WordPress on the back end. Also, I wanted to install it in the web root directory, meaning that both index.html and index.php would sit side-by-side in the same folder. This would allow me to display index.html to all visitors that would reach the site via the domain (http://www.myexmplesite.com/). Then, while developing the WordPress interface, I would be able to simply type in index.php to see the home page of my WordPress installation (e.g. http://www.myexamplesite.com/index.php).

I was able to setup everything like I wanted. After installing WordPress, index.html was displaying by default instead of index.php. I could get to my WordPress admin just fine and any of the pages I created. However, when attempting to access index.php, the site would automatically strip out index.php and redirect me back to the base url: http://www.myexamplesite.com/. By doing so, the site would only display index.html whenever I tried to access index.php.

Solution

I found that I could comment out a line in the /wp-includes/canonical.php file that resolved the issue. The line number is around 284, as seen in the following code:

//wp-includes/canonical.php (around line 284)
//trailing /index.php
//$redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']);

I don’t believe that doing this has any other side effects other than allowing index.php to display in the URL. If I find there are problems caused by it, I’ll update this post at some later date.

Spread the love

Tags: ,

No comments yet.

Leave a Comment

Remember to play nicely folks, nobody likes a troll.

You must be logged in to post a comment.