Support » Plugins » Using /post_id/ as canonical URL

  • The idea is very simple:

    permalinks in the format


    but I want anything after the


    to be optional. That is so say, if someone clicks on a link for


    but it was supposed to be


    I don’t want to serve up a 404, I want them to get post #514. Ideally they could request anything from one extreme:


    to the other


    and still get the post they are after.

    This seems like it ought to be possible, but I’m not sure how to proceed.


Viewing 2 replies - 1 through 2 (of 2 total)
  • I am also interested in what you’re trying to achieve (hence why I found your post), but I want one additional thing. Using your example I’d like it if anything from:



    not only went to the correct post, but did a full redirect to that post’s permalink, like so:


    Perhaps that all was what you were meaning, but since I wasn’t sure I thought I’d throw in my 2-cents, along with my support of what you are seeking to achieve.

    Hopefully someone else with far more knowledge than myself will reply soon to help you (and me) out with this dilemma.


    Here’s what I’ve come up with:

    	$reqURI = explode('/',$_SERVER['REQUEST_URI']); // explode url into an array
    	//if last part of url is a number, then redirect.
    	if(is_numeric($reqURI[count($reqURI) - 2])){
    		header ('HTTP/1.1 301 Moved Permanently');
    		header('Location: ' . get_permalink($post->ID));

    What this is doing is getting the last bit of the URL (the last section between forward slashes) and seeing if it is a number or not. If it is not a number then it does not do anything else (ie, the page loads as normal), but if it is a number then it queries WordPress for the permalink of the post that you’re trying to view (using the number that was evaluated) and then redirects to the permalink address, also returning a 301 at the originally requested shortened address.

    For example, if I try to go to:
    the above script will recognize that the address ends in a number, it will use that number to get the permalink, and it will redirect to:

    It also works if I just type:
    First WordPress automatically redirects to:
    and then my script takes over from there and does it’s thing.

    A couple of things to note:

    1) The above script must be placed in your code before any html (or anything that might interpreted as part of the final page, such as carriage returns outside of PHP tags). Otherwise you’ll get an error that it can’t send/change headers because they’ve already been sent/changed. You’ll probably have to experiement to find how high in your code it has to be to work, but if you can just put it at the very top of your PHP it should work.

    2) I have no idea what problems my method might indirectly cause. I’ve tried to make it efficient (e.g., evaluating the url to decide whether or not it even needs to query WordPress for the permalink), but I am admittedly still quite new at PHP and WordPress especially. So, I don’t know if or how well this method will scale.

    3) My site is set up as
    It is possible that if you have WordPress installed at (without the /blog) that you might need to change the 2 to something else, although I don’t think you should need to. The 2 was what I needed to subtract to get the last bit of the url.

    I would LOVE to get some feedback on this code. What problems do others see that I’m missing? How could things be done better? How could I have come up with something so brilliant (ha ha! I’m joking here, though I’ll certainly accept compliments.)? 😉

    Seriously though, I’d love to get some input/feedback from more experienced coders.


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using /post_id/ as canonical URL’ is closed to new replies.