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.

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:
- Modifying the links themselves using the appropriate WordPress filter hooks, like
post_link,page_link,term_link, etc. For example, let’s say, we modified the links using thepost_linkfilter, and then this modification will be applied every time we usethe_permlaink()function in the loop. - Changing query arguments using the
requesthook. This one is the most important because we let WordPress know that when there is a certain pattern in the page URL, we need to apply non-standard rules. - Performing redirects. Of course, when you change your permalinks, structure, you need to make sure that old permalinks redirect to new ones. Probably, it is not that necessary for new sites.
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 for | Hook |
|---|---|
| Posts | post_link |
| Pages | page_link |
| Custom Post Type | post_type_link |
| Categories, Tags, Custom Taxonomies | term_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
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
Great explanation!!
Would it be possible to create a rewrite rule for a custom post type and categories?
Something like:
CPT = Cars
Category = BMW
Post Name = Sedan
Resulting URL = www.mysite.com/cars/bmw/sedan
Also, would it be possible for a post with multiple categories to generate multiple URL’s? Like, if I have a bmw and mercedes sedan
CPT = Cars
Categories = BWM, Mercedes
Post Name = sedan
Resulting URL = www.mysite.com/cars/bmw/sedan
Resulting URL = www.mysite.com/cars/mercedes/sedan
Thanks!!
Please, how can i make
$_SERVER["REQUEST_URI"]to point to a custom file and not the index.php?Hi Phil,
Not sure, but maybe it will help:
$_SERVER["REQUEST_URI"] = 'custom url';Please can you help me?
If I remove remove the category base /category/ via Yoast SEO I end up with a 404 error.
Hi Romain,
I recommend you to do it with this tutorial. You could also try to go to Settings – Permalinks and just update the settings without changes – sometimes it helps.
Ok thank you I’ll make a try
Another question about your code :)
How can I make it work if I want to change all my categories, taxonomies, tags, posts, pages?
In fact what I really need, is to push a variable, before every permalink on my website, like a translation system
http://www.mywebsite.com/en/category/test,
http://www.mywebsite.com/fr/category/test,
http://www.mywebsite.com/de/category/test…
I have no ready code for your task, sorry :)
Hi sir i want to change a custom post type category link tour-destination to flight-destination is there a way please tell
Hi,
Yes, you have to find
register_taxonomy()function in your website code and make a replacement there.Hello Misha, awesome article!
Hopefully this will help to change my permalinks on single posts.
I’ve been searching for days to find a good answer, but I just can’t seem to find a good solution.
I was wondering, is it possible to use $attachment_title and set that data as a permalink on a single post?
I hope you can guide me in the right direction.
Hello Kevin,
What is $attachment_title ?
The title of an image attachment in WordPress. But you can remove my question, since I’ve hired a developer to do the job for me. Cheers
Ok, good luck!
Hey Alexander,
To be honest I’m not so good at using
add_rewrite_rule()function, and my tutorial is about another method.Hey Misha, That’s really not a problem as i have solve he query and if it is possible i want to write a tutorial on this for your blog that will help the audience to understand easily. Cheers :)
Hey 🙃
Ok, you can send me your draft by email. If I like it, I will publish it with your authorship and all your links inside.
Let me know your email so i will send you draft as soon as possible.
You can do it on this page.
I had set up a taxonomy “authors” using yoast simple taxonomies and now i moved all the data to pods taxonomy ‘author”. The old “authors” is empty.
How could i redirect the old url “authors/” to the new “author/”. Thanks in advance. Any help will be awesome.
You can do it in your
.htaccessfile with this line:Please do not forget to replace rudrastyh.com with your domain.
Thanks a ton for the reply and the solution. It worked like a charm. I have been pecking on this for the past two days. You are just awesome!
Hello,
I need help in creating a custom url.There is one page About-us.I create a post listing under this page.every post has one gravity form. I want to redirect url on the submit.Recently my url has been page_name/post_name and I want to redirect on Thank-you page.So I want page_name/post_name/Thank-you. (Than-you can be anything like page/post/tag/category)
Thanks in advance.
Can I rewrite from: domain.com/?add-to-cart=123
to: domain.com/add-to-cart/name-of-the-product
How is it possible to do? Thanks in advance!!!