In WordPress, rewrite rules allow you to customize the URL structure for pages, posts, categories, and tags to improve SEO or user experience. The following code examples demonstrate how to add a pseudo-static .html suffix to URLs for pages, tags, categories, and custom post types.
Core Rewrite Function
Add the following code to your theme's functions.php file or create a custom plugin.
// WordPress custom taxonomy, tag, and page URL rewrite with .html suffix
function custom_page_rules() {
global $wp_rewrite;
/** Page URL structure **/
$wp_rewrite->page_structure = $wp_rewrite->root . 'page/%pagename%.html';
/** Tag URL structure **/
$wp_rewrite->extra_permastructs['post_tag']['with_front'] = '';
$wp_rewrite->extra_permastructs['post_tag']['struct'] = $wp_rewrite->extra_permastructs['post_tag']['with_front'] . 'tag/%post_tag%.html';
/** Category URL structure **/
$wp_rewrite->extra_permastructs['category']['with_front'] = 'category';
$wp_rewrite->extra_permastructs['category']['struct'] = $wp_rewrite->extra_permastructs['category']['with_front'] . '/%category%.html';
}
add_action( 'init', 'custom_page_rules' );
Code Explanation
- Pages: Rewrites page URLs to the format
/page/sample-page.html. - Tags: Rewrites tag URLs to the format
/tag/tag-name.html. - Categories: Rewrites category URLs to the format
/category/category-name.html.
Note: After making changes, you must visit Settings > Permalinks in the WordPress admin and click "Save Changes" to flush the rewrite rules.
Custom Post Type URL Rewrite
For custom post types (e.g., video), you need to modify both link generation and rewrite rules. The following example rewrites video post URLs to the format /video/{ID}.html.
// WordPress custom post type 'video' rewrite to id.html
add_filter('post_type_link', 'custom_video_link', 1, 3);
function custom_video_link( $link, $post = 0 ){
if ( $post->post_type == 'video' ){
return home_url( 'video/' . $post->ID .'.html' );
} else {
return $link;
}
}
add_action( 'init', 'video_rewrites_init' );
function video_rewrites_init(){
add_rewrite_rule(
'video/([0-9]+)?.html$',
'index.php?post_type=video&p=$matches[1]',
'top' );
add_rewrite_rule(
'video/([0-9]+)?.html/comment-page-([0-9]{1,})$',
'index.php?post_type=video&p=$matches[1]&cpage=$matches[2]',
'top'
);
}
Code Explanation
- post_type_link filter: Alters the permalink generation for the
videocustom post type to output/video/123.html. - add_rewrite_rule: Adds two rewrite rules for the main post page and paginated comment pages.
- Important: After adding this code, you must also visit Settings > Permalinks and save once to flush the rewrite rules.
Common Issues and Notes
- Rule Conflicts: Ensure new rewrite rules do not conflict with existing page or post slugs.
- Flush Rules: You must save the permalinks settings page after any rewrite rule change.
- Performance: Excessive complex rewrite rules may slightly impact performance; use only when necessary.
- Code Corrections: Original code used incorrect quotes; corrected to proper single quotes. Ampersands are correctly escaped for HTML output.