How does WordPress handle URLs?

I encountered this (to me) unfamilliar behavior in WordPress.

Consider a random site using WordPress with a page called example with no content. This is just a page type post and not a category, tag or any sort of hierarchy. A 404 error is triggered if I append characters after the last “/”:

Read More

http://example.com/example/abc

I expect that but if I only type numbers after the final “/” no 404 error is triggered. For example loading:

http://example.com/example/1123456

gets me to example page and the numbers are kept in the URL. The numbers have no special meaning in this case. There could be any nuber there, but I would expect it to trigger a 404 as it does on other sites.

Appending numbers to the URL on index or category pages still seems to trigger a 404, but not on posts and pages.

I am wondering wether this is an actual feature of WordPress and if so, then what is its purpose?

Related posts

3 comments

  1. This is as designed. WordPress matches URL’s through the built-in rewrite module. You can read about it here. In addition, the source code to follow what is happening with this process is located in wp-content/class-wp-rewrite.php.

    The specific line that provides the behavior you are seeing is below.

    [(.?.+?)(?:/([0-9]+))?/?$] => index.php?pagename=$matches[1]&page=$matches[2]
    

    This code will match on page/post-name, and any following number (this supports pagination). However, it will not match on non-characters after the post name. What is interesting is that even if the post does not have the specified page, it will still accept as legit.

  2. Yes, it’d an odd one, but possibly by design.

    http://ww.example.com/mypage/ 200 (expected)

    http://ww.example.com/mypage/idontexist 404 (expected)

    But:

    http://ww.example.com/mypage/0000 200 (unexpected)

    I think it may have to do with how WP works with archive ID’s and dates as part of the permalink structure / rewrites.

  3. As we all knew that WordPress works through built-in rewrite module. So it will also give you Permalinks feature for all your post you can set your permalinks through it. Let’s Check out the detail here about Permalinks through WordPress based blog. Here we go with the link http://codex.wordpress.org/Introduction_to_Blogging#Pretty_Permalinks

    If you want to restructure your all the URL then you need to edit this file.

    $permalink_structure 
    

    The permalink structure as in the database. This is what you set on the Permalink Options page, and includes 'tags' like %year%, %month% and %post_id%.

    For more detail about the permalink structure you can go through this link.
    http://codex.wordpress.org/Class_Reference/WP_Rewrite

Comments are closed.