Add .html extension to wordpress permalinks for posts/pages

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

Post a Comment