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! ;)

This entry was posted in bbPress, BuddyPress, Plugins, WordPress Multisite and tagged , . Bookmark the permalink.

18 Responses to Change WordPress permalinks on pages to have a .html extension

  1. Mal Milligan says:

    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

  2. hugo says:

    hey there, I need to change the pages to php extension, , is it useful for this ?, please, reply me ! thanks

  3. axa says:

    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();
    }
    }

  4. paul says:

    This plugin add the .php at the end of wordpress page

    http://wordpress.org/extend/plugins/php-to-pages/

  5. Gary says:

    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!

  6. Sam says:

    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

  7. Sam says:

    Can anyone please help with my problem?

  8. Blau3grana says:

    Great. I had a similar plugin but it seems to work better with this.
    Thanks!

  9. mutuelle says:

    Bonjour,
    Je vous conseille de lire un site qui parle de mutuelle

  10. Thank you.

    If you get a division by zero error please note to check the ” and ‘ after copy and paste.

  11. Pingback: Better way to use Wordpress as both a CMS and a Blog | PlacementEdge Blog

  12. ken says:

    This is a great plugin!!

  13. Sam says:

    Hi,

    The plugin doesnt work for me, all i get is a 404 page when trying to view the /blog/ page.

    Please Help!!!

  14. munney says:

    Is it easy to modify the plugin to make the extension just .htm instead of .html?

    I’m going to try it!

  15. munney says:

    I should have tried it first but I can now confirm it is very easy to modify the plugin to make the extension just .htm if you need or want to.

    • Sharon J says:

      Thank you for your comment. I have a customer whose site is very old, he wants wordpress, but all his pages are htm. His page rank is 6 and I don’t want to mess with that!

      I am glad it works!

  16. Techkol says:

    its working……thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>