Change WordPress permalinks on pages to have a .html extension

We are working on a new website built with WordPress MU, BuddyPress and bbPress and our client requested that all of the pages and posts of each MU site have a .html extension. Ok, so posts are easy just head to your “Permalink Settings” page, click the “Custom Structure” radio button and add something like “/%postname%.html”. Pretty simple huh?

So what about pages? I found a great plugin called .html on Pages which will add a .html extension to all of your pages. This plugin will also automatically remove the .html extension from any pages that are nested  as a parent page. Soooo: will turn into

We ran into a problem, well not so much a problem because the plugin does what it is supposed to do, but we want to not add .html to the end of the blog page. We want /blog.html to be just /blog so we had to make a few modifications to the plugin. If you are comfortable with php the mod is small and here it is:

Add the following code where ever you want inside of the plugin. This code filters any instance of /blog.html and returns /blog.

function blog_permalinks_page_link($permalink, $page) {
$pos = strpos($permalink, “/blog.html”);
if ($pos !== false) {
$permalink = str_replace(“/blog.html”,”/blog”,$permalink);
return $permalink;
add_filter( ‘page_link’, ‘blog_permalinks_page_link’, 10, 2 );

Next you will need to alter the html_page_permalink() function and add the following code to the top of it:

$pos = strpos($string, “/blog.html”);
if ($pos !== false) {
switch_to_blog(1);//We are using WPMU if you are not you won’t need this line.
wp_redirect( get_option(‘home’).str_replace(“/blog.html”,”/blog”,$string), 301 );
$pos = strpos($string, “/blog”);
if ($pos !== false) {
$_SERVER[‘REQUEST_URI’] =str_replace(“/blog”,”/blog.html”,$string);
global $wp;

The above code makes the redirect of any hits on /blog.html to /blog (which shouldn’t happen because of the link filter) and hard-codes the request-URL so WordPress thinks it’s pulling from the page /blog.html when it’s now on /blog. That’s it, pretty simple… If you are not good with php you can download the modified plugin here:

I’m sure this plugin could easily be modified to work with other extentions like .htm or .php, which could cut down on the amount of 301 redirects you would have to make when migrating to WordPress which by the way everybody running a blog or basic CMS should be doing! ;)

Posted in bbPress BuddyPress Plugins WordPress Multisite and tagged with wordpress permalinks wp .html extension

26 thoughts on Change WordPress permalinks on pages to have a .html extension

Have a comment?leave a reply

Your email address will not be published.