Add .html extension to permalinks

html-extension

Reasons to add .html extension to your Permalinks

1. You have a static html site you want to convert to WordPress but do not want to change the URL names. This is a good reason why you should make WordPress html extensions in terms of permalink structure, if you do not want to do a lot of 301 redirects.
2. In my personal experience, .html pages rank higher than my normal WordPress pages. This could be wrong for many reasons. So I can not say 100% that .html WordPress pages rank higher or are better for SEO.
3. People say it looks better. It feels and looks static and some people view this subconsciously with a little more trust than a dynamic page.

 


By default, .html extension is possible on posts but not on pages. So today I am going to show you how you can add .html extension to both posts and pages in WordPress

Adding .html Extension to Posts

Go to Settings -> Permalinks then select the “Custom Structure” radio button. Change the structure of the code to:

1
/%postname%.html/

Done!

Adding .html Extension to Pages

Add this block of code into your functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
add_action('init', 'html_page_permalink', -1);
register_activation_hook(__FILE__, 'cvf_active');
register_deactivation_hook(__FILE__, 'cvf_deactive');


function html_page_permalink() {
   
    global $wp_rewrite;
   
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
    }
   
}
add_filter('user_trailingslashit', 'no_page_slash',66,2);
function no_page_slash($string, $type){
   
    global $wp_rewrite;
   
    if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){
        return untrailingslashit($string);
    } else {
        return $string;
    }
   
}

function cvf_active() {

    global $wp_rewrite;
   
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
    }
    $wp_rewrite->flush_rules();
   
}

function cvf_deactive() {

    global $wp_rewrite;
   
    $wp_rewrite->page_structure = str_replace(".html","",$wp_rewrite->page_structure);
    $wp_rewrite->flush_rules();
   
}


Do you need help with a project? or have a new project in mind that you need help with?

Contact Me