How to create URL’s like what wordpress does without having a physical page file?

My first question at Stack Overflow. This site has been a huge resource of answers for me, so I thought I should just go ahead and join, but anyway…

I need to create normal urls like what wordpress does. Ex: And have them load pages from the database, not just static page files. But like how wordpress and other cms’s do.

I’m well aware of the use of url parameters, such as /mypage?ID=124&fname=John But those are sloppy and can also invite hackers. And I don’t want that stuff in my urls if I can avoid it.

How do they have a url to a page that only exists in the database? How do you connect to something that displays in the browser without having an actual “mypage” file and still have these clean urls?

I just can’t figure out how it’s done. I’ve messed around a bit with .htaccess things, but I’m not real educated on htaccess stuff.

I suppose I should add that I am working with php7 and MySQL database.

Any assistance, or resources would be much appreciated. 🙂


You can simulate it by redirecting traffic on an entry page (for example an index.php). Then do some logic based on the requested URL to render the actual page based on $_SERVER["QUERY_STRING"] or other url parameters you can use such as the $_GET global variable.

You can redirect traffic to a single entry point by using an .htaccess such as this on apache.

RewriteEngine On

RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

Options -Indexes

I created a personal script that actually do this and if you are interested, you can take a look on it’s underlying logic here at github.

There are cons on using this such as handling a 404 page request on your own routing.

After being redirected to index.php and assuming that your URL looks like this

You can add a switch case to render a page based on that variable such as this:


$page = isset($_GET["page"]) ? $_GET["page"] : 0;

switch($page) {

  case 1:
    require "sample/page/if/one/page.php";
  case 2:
    require "sample/page/if/two/page.php";
    require "default/page/home.php";

That is a quick and dirty example but can be greatly improved by creating a router class. You can also create an array of possible pages and check if the requested page is in that array then render it and if not, display your 404 page.

I am just showing a very minimalist example using the $_GET global variable to require some pages but this can also be used to fetch dynamic data from DB by utilizing the variable’s value.

Leave a Reply

Your email address will not be published. Required fields are marked *