How to get rid of index.php to make the pretty permalinks in WordPress

Permalink is very important. It’s one of the first things that you need to configure when you build up a website. Not only does it provide you a more comprehensive url but it also helps you in the search engine optimization (SEO).

WordPress has this feature with 4 options built in out of the box. What you want to choose is either “Day and name” and “Month and name” because they provide a better results.

image

But things will be getting complicated when you want a pretty permalink structure that does not have the annoying “index.php” in it, because then you need to go with a “custom structure” with different setup based on what type of platform that runs your WordPress. Typically, the pretty permalinks are available under:

For this blog in particular, I had to use a 404 handler because this blog is hosted on IIS 6.0 web server. And here are the steps:

1. Create the file wp-404-handler.php in my WordPress root folder with the following php code in it.

<?php
$_SERVER['REQUEST_URI'] = substr($_SERVER['QUERY_STRING'], strpos($_SERVER['QUERY_STRING'], ‘:80′)+3);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
include(‘index.php’);
?>

2. Set my site’s 404 page to point to the wp-404-handler.php either in url or file format. It would be difficult if you don’t have the access to the control panel that gives you the ability to change the setting. Note that, based on the hosting package, error redirecting may not be an option to you.

3. Go to Permalink set up page on WordPress control panel, and set it as

/%year%/%monthnum%/%day%/%postname%/

For more details, check out this page.

You think I got it right by now? Not really, ever since I turned this format on, all the pagination on home page, search results page, and archives page stopped working properly. They all got wp-404-handler showing up in the url basically saying this url was broken. It’s definitely due to the change made on the permalink because it rewrites the output from get_pagenum_link function by adding error handler. Not sure if that’s just only me but here is what I did to solve this issue.

Because I use a plug-in called WP Page Numbers, I dig into the plug-in folder and modified the wp-page-number.php file by

1. adding a function called cm_page_link_fix($pageLink)

function cm_page_link_fix($pageLink)
{
    if (strpos($pageLink, "/wp-404-handler.php")>0)    {
        $pageLink = str_replace("/wp-404-handler.php","",$pageLink);
        return substr($pageLink,0,strpos($pageLink,"?"));
        }
    else
        return $pageLink;
}

2. replacing all get_pagenum_link() with cm_page_link_fix(get_pagenum_link()).

From now and on, I will have to remember either not upgrading this plug-in or modifying the change every time after an upgrade.

If you are using the different pagination plug-in, I believe you should be able to do the same.

That’s it. Everything works nicely now.