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: yourwebsite.com/mypage.html will turn into yourwebsite.com/mypage/mysubpage.html.
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:
$string=$_SERVER['REQUEST_URI'];
$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 );
exit();
}else{
$pos = strpos($string, “/blog”);
if ($pos !== false) {
$_SERVER['REQUEST_URI'] =str_replace(“/blog”,”/blog.html”,$string);
global $wp;
$wp->parse_request();
}
}
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: http://webdevstudios.com/downloads/html-on-pages.zip
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!
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader or join our newsletter and receive future articles emailed directly to you.
Comments
hey there, I need to change the pages to php extension, , is it useful for this ?, please, reply me ! thanks
Many thanks Brian, you did a good job. I added this condition strlen($string)<=strlen('/blog/') because my posts Permalink contains '/blog/'.
$string=$_SERVER['REQUEST_URI'];
$pos = strpos($string, "/blog.html");
if ($pos !== false)
{
wp_redirect( get_option('home').str_replace("/blog.html","/blog",$string), 301 );
exit();
}
else
{
$pos = strpos($string, "/blog");
if ($pos !== false && strlen($string)parse_request();
}
}
A client rang me up saying that he’d got WP to add the .html extension to pages, but want /blog.html to be just /blog.
Within a minute of searching I’d found your page, and it does *exactly* what I needed. Kudos – thanks for sharing!
Hi,
Great! have had this problem for two weeks now, new problem though my links have changed to mysite.com/blog/ which i wanted, but now i get a 404 error? for the blog, but not for the posts inside it?
Any help would be appreciated
Thanks
Thank you.
If you get a division by zero error please note to check the ” and ‘ after copy and paste.
[...] on pages” plugin adds .html to all pages created in wordpress. I am using a variant of the modified version by Brian Messenlehner that excludes the blog page from being modified. His version used /blog, I [...]
Hi,
The plugin doesnt work for me, all i get is a 404 page when trying to view the /blog/ page.
Please Help!!!



Hey Brian – this is brilliant – thanks. I believe Matt Cutts has mentioned he personally likes to see .html extensions, but the pages should get crawled as long as they don’t end in .exe or .dll
from his blog: “Sometimes at a conference people will ask me “Does it matter what extension I use for my pages? Does Google prefer .php over .asp, or .html over .htm?” And my answer is “We’re happy to crawl all of these file extensions. It doesn’t matter what you choose between any of those.””
But I’ve never seen a WP site converted for all .html extensions. I wonder what the impact to ranking might be? Anyway – food for thought. I’m going to experiment with it.
Cheers – Mal