Loading WordPress From index.php

One of WordPress’ strengths is its attention to SEO-related issues in its core files. One of those issues is the problem of having the home page of the blog indexed twice in the search engines; once under the actual address, http://domain-name.com/index.php, and the other as the plain domain name: http://domain-name.com. Note that this is a different problem than the trailing slash problem ( http://domain-name.com/ vs. http://domain-name.com ) which WordPress also takes care of.

WordPress handles the index.php problem by rewriting requests for http://domain-name.com/index.php to http://domain-name.com. All well and good, and beneficial for most sites.

But that rewriting/redirecting caused some problems on a site I was working on yesterday, and once I figured out how, it was a relatively easy fix.

Here’s what happened: a client had me upgrade an old installation of SemioLogic’s version of WordPress to genuine WordPress. While it can be time-consuming, switching over is a fairly straightforward process most of the time. The challenge here was that while most of the site is normal .html files, WordPress is installed at the root level, and is not actually serving the ‘home’ page of the site.

So you can maybe see where this is headed: the ‘home’ page of the site is index.html. That’s what comes up when you ask for http://domain-name.com. The server is set to look for index.html first, then index.php if index.html isn’t there. So to get to the blog, you had to ask for http://domain-name.com/index.php.

But when you asked for index.php, WordPress, being the dutiful SEO-friendly software that it is, stripped off “index.php” from the request, and redirected to http://domain-name.com.

The server saw the request for the site index file and promptly served up index.html. So you couldn’t get to the home page of the blog. If you had a specific post URL and typed it in, it worked fine.

Easy fix, says I. Settings -> General, change the WordPress url to http://domain-name.com/index.php from http://domain-name.com.

Oops. Now all the permalinks have ‘index.php/’ prepended: http://domain-name.com/index.php/i-want-this-post. Not good, and not intended, especially as the site has been indexed in Google without the index.php in there.

I never did figure out how SemioLogic handled this; obviously it was working before the changeover. Undoubtedly there was an easy setting that disappeared once the SL files were gone. I can only think this issue had come up before and the author of SL provided a workaround.

Thankfully, the coders of WordPress also recognized that there may be a time when rewriting URLs wasn’t good so they provided a filter to disable or alter the rewrite. Once I found that notation in includes/canonical.php, the fix was a breeze. Write a plugin that disables the redirect to / when /index.php is called for. Here is the entire plugin:

<?php
/*
Plugin Name: Index.php fix
Plugin URI: http://ilikewordpress.com/loading-wordpress-from-index-php
Description: This plugin allows a blog installed at root to be addressed by /index.php. Remedies stripping of filename by includes/canonical.php
Author: Steve Johnson
Version: 1.0
Author URI: http://ilikewordpress.com/
*/

/*
*    Applies filter to redirect_canonical to defeat
*    stripping of index.php file
*/

function fix_index( $requested_url ) {
 if ( get_bloginfo( 'url' ) == $requested_url )
 return false;
}
add_filter( 'redirect_canonical', 'fix_index' );

?>

And that’s all there is to it. Now when a browser asks for ‘index.php’, that’s what it gets instead of a redirection to /.

You could also put this in the functions.php file of a theme, but obviously it wouldn’t work if the theme were changed.

Comments

    • Steve says

      Yasir, you shouldn’t be having redirect problems at all. You’ll need to access your dashboard at yoursite.com/new-wp-folder/wp-admin, though, you won’t be redirected to the new folder automagically.

  1. says

    You saved my day. This is awesome. Was considering regex to fix this so I could work on a WordPress site next to a current .html based site. This filter is way better!

  2. martin says

    hi,

    thanks for that plug in^^
    as i used it few months ago on a canvasdreams hosting with wp 3.2.1..everything worked well like described
    now i try to do the same with wp 3.3.1 and another canvasdreams hosting account…but it doesn`t work enymore as the first time

    without the hook i can only reach index.html…and with the hook i can reach both html and php but the html only with the full adress to the html file
    my intention is to have an under construction html page, until i am ready for going online, but i don`t want to work first in a draft folder or something like this

    can you help me?

    here my htaccess..if it helps:

    # -FrontPage-

    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

    order deny,allow
    deny from all
    allow from all

    order deny,allow
    deny from all

    AuthName smallbizmarin.org
    AuthUserFile /home/smallbm2/public_html/_vti_pvt/service.pwd
    AuthGroupFile /home/smallbm2/public_html/_vti_pvt/service.grp

    • martin says

      ahhh, ok i got it^^…i had a custom permalink structure, no i switched it to default and everything works^^

  3. says

    Good Day;

    After copying your script to a file I labeled URLREWRITE.PHP and zipping. I used PLUGINS, ADD NEW PLUGINS to upload the file. That after ACTIVATING I found that I received”

    Warning: Cannot modify header information – headers already sent by (output started at /home/content/22/7568022/html/commodore/wp-content/plugins/ian-urlrewrite/ian-urlrewrite.php:24) in /home/content/22/7568022/html/commodore/wp-includes/option.php on line 568

    Perhaps I missed a step in your process. As an intermediate WP user I had not “written” a plugin before but your article pointed me to the right direction so I wanted the thank you for that.

    In the end I removed this plugin and used “Fix Multiple Redirects 1.2” in which I set “redirect_canonical” and life is now fine.

    Now can spend a few months re-writing http://www.commodore.ca while keeping the old site live.

    Thanks for your efforts.

Trackbacks

  1. […] same project that led to the post Loading WordPress From index.php involved cleaning up after a hacking incident. In fact, that’s what the initial work order […]

  2. […] the original:  Loading WordPress From index.php browser, browser-privacy, can-use, could-not, fix-this, invalid-request, key-shown, multitouch, […]

Leave a Reply