Add .html extension to wordpress permalinks for posts/pages

March 21st, 2015 Raj 7 Tips

There are many time that you want to add different permalinks to your wordpress site, maybe you have some other cms and you want to transfer to wordpress, and your previous blogging platform has some other permalink structure and you want to make sure that your new blog has the same extension type. Or you want to beautify your linking structure. To achieve that wordpress gives you 5 predefined options in permalink, along with that you can custom define permalink structure like you want.

1) Default http://example.com/?p=123

custom structure blank

2) Day and name http://example.com/2015/03/21/sample-post/

custom structure /%year%/%monthnum%/%day%/%postname%/

3) Month and name http://example.com/2015/03/sample-post/

custom structure /%year%/%monthnum%/%postname%/

4) Numeric http://example.com/archives/123

custom structure /archives/%post_id%

5) Post name http://example.com/sample-post/

custom structure /%postname%/

If you are not satisfied with these options or want your site to have different linking structure then there are other ways to do custom permalinks.

Adding .html to posts

Add the following code to custom structures in permalink.
1) Day and name http://example.com/2015/03/21/sample-post/

custom structure /%year%/%monthnum%/%day%/%postname%.html

2) Month and name http://example.com/2015/03/sample-post/

custom structure /%year%/%monthnum%/%postname%.html

3) Numeric http://example.com/archives/123

custom structure /archives/%post_id%.html

4) Post name http://example.com/sample-post/

custom structure /%postname%.html

Adding .html to pages

Adding .html extension to pages requires some hack into the theme function.php file. You would need to add the following code to the function.php file.

add_action('init', 'change_page_permalink', -1);
function change_page_permalink() {
    global $wp_rewrite;
    if ( strstr($wp_rewrite->get_page_permastruct(), '.html') != '.html' ) 
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
		$wp_rewrite->flush_rules();
}

7 Responses to “Add .html extension to wordpress permalinks for posts/pages”

  1. GeorgeFab says:

    This post is truly amazing!

  2. Richard says:

    Hi Raj,

    For adding .html to Pages (not posts), I've simply edited the permalink on the page by adding .html. WordPress changes it to -html. However, when your enter pagename.html in the browser the page is displayed as expected and the browser displays .html.

    Are there any problems or disadvantages with doing this?

    What are the advantages of adding your code to the functions.php file.

    I am converting an old HTML site to a WordPress site and I want to maintain backlinks and SEO juice.

    Thanks!

    • Raj says:

      Hi Richard,
      When you add say "dummy post.html" to pages (in the title field), it's converted by wordpress to the following slug "dummy-post-html" this is your post url, not "dummy-post.html", search engines sees the former. Actually you haven't changed any thing in the permalink, its just some fancy title.

      If you use that you would be loosing your backlink juice. To maintain the backlinks you either need to set the same url (by using the above post as guidelines) or use 301 redirect from old urls to new ones.

      Regarding the page been shown when you enter "dummy-post.html" in the browser, its because wordpress redirects that url via canonical to "dummy-post-html", in my view that won't help your cause.
      Regards,
      Raj

      • Richard says:

        Thanks Raj for the explanation. I will use your recommended code from the post.
        Expect Success,
        Richard

      • Richard says:

        Hi Raj,
        I used your code and it worked fine….. until I installed a plugin that created pages and broke apparently because of the code.

        So then I removed the code from functions.php and all of my pages returned a 404 error. I changed themes and still got 404 errors.

        Is there some way to "remove" the settings from your code even after it's been removed from functions.php?

        Thanks,
        Richard

        • Raj says:

          Hi Richard,
          Its strange that the plugin broke things for you.

          I wished you have mentioned more details about the plugin used, that broke the stuff for you. Anyways there is a simple action that works to fix the permalink structure, you can try this, head over to permalink settings on your blog. settings >> permalink link in your blog admin sidebar, without changing any options hit the "save changes" button. Performing this action will help clear the unwanted permalink cache from your site. You can do this after activating and deactivating plugins from your site too.

          Let me know if that fix stuff for you, or alternatively you can contact me through the contact us link in this site, and we can discuss things quickly over google or yahoo chat.
          Regards,
          Raj

  3. It's really a great and helpful piece of information. I
    am happy that you just shared this helpful info with us.

    Please keep us informed like this. Thank you for sharing.