Menu Close

How to transform PHP dynamic URL format to static

techwetrust post image

It is pretty easy to set up a simple PHP website and use the switch case pattern for path handling. You’ll get to the following dynamic method of accessing your PHP files, by passing a query string e.g. yourwebsite.com/index.php?page=contact.

But How can you mask it or transform it into a static path like /contact?

Apache webserver – .httaccess

Pretty simple, if you are using Apache as a web server, you will have to use a .htaccess file for extra rewriting rules.

If you want to transform the dynamic query URL format, http://example.com/index.php?q=contact to a simpler version http://example.com/contact then you’ll have just to add a simple rewrite rule to the .htaccess file.

RewriteEngine On
RewriteRule ^(.*)$ /index.php?q=$1 [L]

Make sure the RewriteEngine is On, if not, add the line above.

Nginx

You can add some simple logic for Nginx in its conf.d/default.conf file or on your server configuration file. The following block of code will do the trick.

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }
Spread the love

Leave a Reply