Remove .php Extension In Apache

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.

URL Rewrite Using Lighttpd

You will need to enable the mod-rewrite in Lighttpd and then open up the file under /etc/lighttpd/lighttpd.conf

$HTTP["host"] =~ "" {
  url.rewrite-final = (

    # Exclude some directories from rewriting
    "^/(wp-admin|wp-includes|wp-content)/(.*)" => "$0",

    # Exclude .php files at root from rewriting
    "^/(.*.php)" => "$0",

    # Handle permalinks and feeds
    "^/(.*)$" => "/index.php/$1"

This configuration should work well with WordPress installation using permalinks.

URL rewrite support for Lighttpd server

I was setting up WordPress permalinks with Lighttpd but encounter some problems: mod_magnet module was not available for the Lighttpd installed on my VPS. It turned out that I need to install the module manually and then restart the server:

user@server$ sudo apt-get install lighttpd-mod-magnet
user@server$ sudo lighty-enable-mod magnet
user@server$ sudo /etc/init.d/lighttpd force-reload

For the permalink to work I did the following.

Add this piece of code to /etc/lighttpd/lighttpd.conf

$HTTP["host"] == "" {
    server.document-root = "/sites/"
    magnet.attract-physical-path-to = ( server.document-root + "/rewrite.lua" )

And then

attr = lighty.stat(lighty.env["physical.path"])

if (not attr) then
lighty.env["uri.path"] = "/index.php"
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]

to the rewrite.lua under the server document root directory and it should be up and running.