Skip to content
Advertisement

How does WordPress convert directory requests into database generated pages?

This is a curiosity based question, rather than being related to any issue.

I wondered how WordPress rectifies pseudo URL addresses (e.g. blog.com/posts/2015/05/05/example_blog_post) to php generated pages? What are the mechanics behind this redirection process?

Advertisement

Answer

This is actually a rule from the server config, not the CMS specifically. For instance, the common Apache config is:

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

The second-to-last line if “if URL does not exist as a directory, also pass this on to WordPress”.

The rest is handled by the rewrite engine which is a bunch of compiled regular expressions. If you have the WordPress CLI you can run wp rewrite list and it will show you the entire list in priority order:

match query source
(.?.+?)/embed/?$ index.php?pagename=$matches[1]&embed=true page
(.?.+?)/trackback/?$ index.php?pagename=$matches[1]&tb=1 page
(.?.+?)/page/?([0-9]{1,})/?$ index.php?pagename=$matches[1]&paged=$matches[2] page
(.?.+?)/comment-page-([0-9]{1,})/?$ index.php?pagename=$matches[1]&cpage=$matches[2] page
(.?.+?)(?:/([0-9]+))?/?$ index.php?pagename=$matches[1]&page=$matches[2] page
[^/]+/attachment/([^/]+)/?$ index.php?attachment=$matches[1] post
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement