Field Notes

Redirecting index.html requests to a WordPress post

May 11, 2014 | In wordpress

Google Analytics shows my WordPress site gets a lot of bad requests to "index.html".
My permalinks are set to chop the filename off the end of the URL, so the request fails.

This can be fixed with a new .htaccess rule. The .htaccess file gives the web server special instructions on how to direct a request. WordPress uses it to direct all requests to /index.php, its starting point.

The .htaccess file is in the directory where you installed WordPress.

1) Make a backup of the .htaccess file before making changes.

2) Open .htaccess in a text editor. It will already contain a section that starts with #BEGIN WordPress.

3) Insert this before the WordPress section:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteRule ^/?(.*)/index.html$ /$1/ [R=301,L]

The rewrite rule says to convert any request for "/blah/blah/blah/index.html" to "/blah/blah/blah" and make the browser load the new URL.

The flags in the square brackets fine tune the redirect. The "R" flag sets HTTP code 301, moved permanently, which will tell search engines to update their record. The "L" flag makes the web server send the reply back immediately without looking at the rest of the .htaccess file.

Now that the links are fixed, there's an upswell of spam. I disabled comments and pingbacks on old posts until a better solution comes to me. Bulk edit is a wonderful feature.