Blog / WordPress/ How to Add .html Suffix to WordPress Page, Category & Tag URLs

How to Add .html Suffix to WordPress Page, Category & Tag URLs

WordPress Link Suffix Overview

By default, WordPress page, category, and tag URLs do not include a .html suffix. Some site owners prefer to add this suffix for aesthetic consistency, to make pages appear more static, or for potential SEO benefits. This guide explains how to implement this functionality through code.

Implementation Method: Modify Permalink Structure

You can rewrite WordPress's permalink rules to add a .html suffix to page, category, and tag URLs by adding custom code to your theme's functions.php file.

Steps and Code

Add the following code to the end of your active theme's functions.php file:

/**
 * Add .html suffix to page, tag, and category links
 */
function custom_permalink_html_suffix() {
    global $wp_rewrite;

    // 1. Add .html suffix to page links
    // Format: /sample-page.html
    $wp_rewrite->page_structure = $wp_rewrite->root . 'page/%pagename%.html';

    // 2. Add .html suffix to tag links
    // Format: /tag/wordpress.html
    $wp_rewrite->extra_permastructs['post_tag']['with_front'] = false;
    $wp_rewrite->extra_permastructs['post_tag']['struct'] = $wp_rewrite->root . 'tag/%post_tag%.html';

    // 3. Add .html suffix to category links
    // Format: /category/wordpress.html
    $wp_rewrite->extra_permastructs['category']['with_front'] = false;
    $wp_rewrite->extra_permastructs['category']['struct'] = $wp_rewrite->root . 'category/%category%.html';
}
add_action('init', 'custom_permalink_html_suffix');

Code Explanation and Important Notes

  • Function: This code modifies the permalink structure for pages, post tags, and categories to append a .html suffix.
  • Flush Rules: After adding the code, you must visit Settings > Permalinks in your WordPress admin and click "Save Changes." This refreshes the rewrite rules and activates the new URL structure.
  • Backup: Always back up your functions.php file before making changes.
  • Child Theme: If you use a child theme, add the code to the child theme's functions.php to prevent loss during parent theme updates.
  • SEO Impact: Changing URL structures will cause old URLs to return 404 errors. You must set up 301 redirects from old URLs to new ones to preserve search engine rankings and user experience.

Frequently Asked Questions

1. Getting 404 errors after adding the code?

This usually means the rewrite rules were not flushed. Ensure you visited the Permalinks settings page and clicked "Save Changes."

2. How to add the suffix to only one type (e.g., pages)?

If you only want the suffix for pages, keep only the $wp_rewrite->page_structure line and remove the tag and category sections.

3. Can I customize the link structure?

Yes. You can modify the structure strings (e.g., 'page/%pagename%.html'). For example, to have page URLs directly in the root (like /about.html), remove the page/ prefix. Be cautious, as this may cause conflicts with posts or other content.

Summary

Using the method above, you can easily add a .html suffix to WordPress page, category, and tag URLs. The key steps are correctly modifying the rewrite rules and flushing the permalink cache. Remember to handle any permalink changes carefully and implement proper redirects for old URLs to protect your site's SEO and user experience.