How does WordPress create pages without actually creating any folders on the server?

by febin mathew   Last Updated September 09, 2016 09:01 AM

I have a website built on WordPress. I have been thinking for a while how the pages are actually created by the WordPress. The post pages as well as the other single pages that are created from the admin control panel results in a webpage without WordPress actually creating any folder or directory on the server.

For example, the address of the post is www.example.com/test-post/. But there are no separate folders for any pages or post created from the admin panel.

How is this done?



Answers 2


URLs (what you type into the browser's address bar) and file system paths are two different things. Whilst URLs can map to file system paths, this is not always the case. And with WordPress and many popular database driven CMSs these days, this is not the case.

When you make a request for /test-post/ on a WordPress site, WP intercepts the request and internally directs it to /index.php (the physical file on the file system that actually handles the request). This is entirely hidden from the user; the user only sees the URL in the address bar. WP then looks at the URL (ie. /test-post/) and decides what to do with it (ie. route it)... creates a page and sends the response.

Without WordPress intercepting the request, the web server will indeed try and map the URL to a file system path. A request for /test-post/ will instruct the web server to look for a directory called /test-post. If that directory exists, then it will look for a default index document inside that directory (eg. index.html). If nothing has intercepted the request and if the URL does not map to a file system path then the server will return a 404 Not Found (or 403 Forbidden in some cases).

DocRoot
DocRoot
September 09, 2016 09:41 AM

WordPress implements the front controller design pattern. That is, one file handles all the requests for all URLs. Rather than the server figuring out which file to use based on the URL, the server hands all request to a central handler that then programatically decides what content to use.

This handler is named index.php in WordPress. It uses rewrite rules in .htaccess to send every request to that controller. The rule that WordPress uses has exceptions such that files that exist on the server take precedence. WordPress then handles all the other URLs.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Once index.php is handling the request, it can examine the URL path, do a database query, and see if there is content for it.

Stephen Ostermiller
Stephen Ostermiller
September 09, 2016 10:21 AM

Related Questions



Need Help to Change The Header Color Of My Blog

Updated July 22, 2016 08:01 AM


Sudden ranking drop after WordPress theme change

Updated April 03, 2018 02:04 AM