If you have a small site without any framework that provides nice rewrite rules for you, but you still want to hide the “.php” extension from the rest of the world, then simply add the following lines into your virtual host configuration file under Apache or .htaccess file under the root of your host:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And if you want to add the trailing slash to the URL, use the following code instead:
# the following deals with the removal of the extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

# addition of the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
For more information regarding apache’s rewrite module, please visit here. Of course this would require the rewrite module to be enabled on the hosted server which I won’t cover in this post, please refer to Apache’s online documentation.

Leave a Reply

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