Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Wordpress handle URLs?

Tags:

php

wordpress

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 "/":

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?

like image 817
PeterTheLobster Avatar asked Sep 14 '26 18:09

PeterTheLobster


1 Answers

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.

like image 53
Gary Avatar answered Sep 17 '26 09:09

Gary