How to Change the Permalink for One WordPress Page Only?

In this tutorial, I’d like to guide you step by step, how you can change the URL (permalink) of a specific WordPress page without going to Settings > Permalinks and applying the changes globally.

Please keep in mind, that I’m not talking about changing page slugs – that can be easily done in the WordPress admin dashboard (check the screenshot below), I mean changing the permalink structure completely.

WordPress change permalink for one page in admin dashboard
Here is where you change a WordPress page permalink, or to be more specific – slug, in the admin dashboard.

By the way, you can find a couple more tutorials on my blog where I discuss different ways of how you can modify WordPress permalinks programmatically. For example, I already showed how to remove a taxonomy slug from URLs or how to change the permalink structure for a custom post type.

If you read those posts, you may recall that permalink customization consists of the following steps:

In this tutorial I will guide you through all these steps while changing a permalink for one specific page on your WordPress website.

At the moment of writing this tutorial, no other examples came to mind except adding a “.html” extension to some specific pages on a website. But if you have some other interesting suggestions (maybe you would like to include a custom field in the page URL, for example), please let me know in the comments section.

I am going to show you how to make these replacements for a specific page.

Let’s start with changing the URLs which are generated by WordPress and usually returned with functions like get_permalink() and the_permalink().

add_filter( 'page_link', function( $url, $page_id ) {

	// replacement for a specific page only
	if( 123 === $page_id ) {
		$url = untrailingslashit( $url ) . '.html';
	}
	return $url;

}, 99, 2 );

From the above code you can understand that if a page ID is “123”, we’re going to add .html at the end of its URL. It is not necessary to check the page ID by the way, you can also check the page slug, but in that case, we need to get it this way: get_post( $page_id ). For me it seems like an overcomplication here.

If you’re going to do it not only for WordPress pages but maybe for some other entities like tags, custom taxonomies, custom post types, etc, please take a look at the table below:

Changing permalinks forHook
Postspost_link
Pagespage_link
Custom Post Typepost_type_link
Categories, Tags, Custom Taxonomiesterm_link

What do you think will happen if we visit our page by the URL domain/sample-page.html? Be ready for a 404 error, of course.

Now, the most important step – we need to let WordPress know that sample-page.html is actually a page with the sample-page slug! We can do it in the request filter hook.

add_filter( 'request', function( $query ){

	// you can also check $_SERVER[ 'REQUEST_URI' ] if you want
	if( isset( $query[ 'name' ] ) && 'sample-page.html' === $query[ 'name' ] ) {
		$query[ 'pagename' ] = 'sample-page';
		unset( $query[ 'name' ] );
	}
		
	return $query;

} );

Last but not least, we must provide 301 redirects from an old URL to a new one. It is not exactly necessary, especially if the page is new, but I strongly recommend doing it.

add_action( 'template_redirect', function() {

	if( str_ends_with( urldecode( $_SERVER[ 'REQUEST_URI' ] ), '/sample-page' ) ) {
		wp_safe_redirect( site_url( 'sample-page.html' ), 301 );
		exit;
	}

} );
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X