How to Change the WordPress Default Permalinks for Pages

WordPress permalinks are not nearly as flexible as I would like them to be. It offers the ability to easily change the Post permalink structure, as well as the category and tag structure, but that’s where it ends.

You have the option of using a plugin like Custom Permalinks, if you want the ability to quickly change the permalink of any term (Tag, Category, Page, etc).

However, WordPress doesn’t offer the ability to change the Page permalink structure.

After reading hundreds of posts, support threads, and articles about $wp_rewrite, I finally developed the code that worked.

The Code

By default, WordPress uses this as the Page permalink structure: /%pagename%

But let’s say you wanted to make it /page/%pagename%/ or /%pagename%.html or something of that variation.

In this example, we will change the permalink structure to be something like this: /page/credits.html (credits being the page slug)

Put this code in your functions.php file.

add_action( 'init', 'custom_page_rules' );

function custom_page_rules() {
	global $wp_rewrite;
	$wp_rewrite->page_structure = $wp_rewrite->root . 'page/%pagename%.html';	
}

If you want to change it to be just /credits.html, you could use something like '%pagename%.html' and so on, and so on.

The few lines of code should be flexible enough to tweak and make your own.

If you have any feedback or questions about, would be happy to hear about it in the comments.

Comments

  1. By on

    Hi, this is super useful, but there is one thing that bothers me. If I set rewrite rule to be /%postname%/, the generated page link is /page/credits.html/, the trailing slash in the end is automatically added by WordPress unless I change the permalink to be /%postname.
    Do you know how I can remove the trailing slash without changing the permalink format?
    Thanks in advance.

    Reply »

    • By Jonathan Dingman on

      I spent a lot of time trying to figure that out, but unfortunately, WordPress over-rides any permalink structure with the existing trailing slash or lack of trailing slash, that’s in your Custom Permalinks.

      If you want to have just /page/%postname, then you do need to set it to /%postname% without the trailing slash.

      If I find a way to circumvent this, I’ll post about it.

      Reply »

      • By on

        Hi, thank you for your reply. Fortunately, I did find something useful regarding this issue, please check the post here http://betterwp.net/wordpress-tips/remove-trailing-slash-for-permalinks-with-extensions/
        It turns out that the canonical redirection function is what caused this issue and we can cancel it via the “redirect_canonical” filter by returning false. I think adding this snippet will make your method perfect!

        Reply »

        • By Jonathan Dingman on

          Good find! I’ll do some more testing next week and see about adding it to the snippet. Thanks Sola!

          Reply »

          • By on

            I’m glad you will see to it, I love your code a lot! Looking forward to your new posts!

  2. By Jonah on

    Nice post, maybe this method can fix our issue. We have all url’s without the “/” on the end like: /%postname%

    Everything is working fine except the news pages, there we miss the “/” in the url so

    “http://ourdomain.com/news/page2/newsitem30” becomes :

    “”http://ourdomain.com/newspage2/newsitem30” which is obviously not working ..

    We dont want slash on the end of our urls, but we need it in the url for news pages..

    Is there any workaround to get this working?

    Thanks in advance!

    Reply »

Leave a Reply

Your email address will not be published. Required fields are marked *