Web Applications Development » Miscellaneous

ID #1020

How can I remove the .php extension of my files with Apache and lighttpd?

On the web, URLs are not very friendly, but it's been a trend with the Web 2.0 wave to make them more friendly and easier to remember. You can for instance hide the file extension, which takes very little effort and makes your URL a little shorter:

http://www.example.com/contact.php => http://www.example.com/contact

To do so, you need a web server like Apache or lighttpd. You don't have to rename your files, keep them with the .php extension for instance. What you need to do in Apache is create a .htaccess file and drop it into the public_html (or www) directory along with your .php files. In this file you just have to paste the following two lines:

RewriteEngine On 
RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php

Save either using the ISO-8859-1 or Windows 1252 encoding (UTF-8 isn't supported yet). In lighttpd, you only have to open the lighttpd.conf file, uncomment the "mod_rewrite" line and paste:

url.rewrite = ( "^([^\.\?]+)(\?.*)?$" => "$1.php" )

When you access your .php files without typing the extension, it should work. If nothing happens, check with your system administrator if Apache is compiled with mod_rewrite support. If you get an "Internal Server Error", make sure you saved with the proper character set.

So, to sum up, when you typed http://www.example.com/contact.php, you can now just type http://www.example.com/contact and Apache or lighttpd will stealthily call the respective PHP file for you.

Tags: -

Related entries:

Last update: 2007-02-02 18:12
Author: Charles A. Landemaine
Revision: 1.0

Digg it! Print this record Send to a friend Show this as PDF file
Propose a translation for Propose a translation for
Please rate this entry:

Average rating: 4.44 out of 5 (9 Votes )

completely useless 1 2 3 4 5 most valuable

You cannot comment on this entry