how to hide .php from address bar

World picture World · Apr 29, 2011 · Viewed 74.3k times · Source

I use php to build web applications, but i want my web pages without .php extension in the browser's address bar. For eample http://www.example.com/index.php shows like http://www.example.com/index in the browser's address bar.

How can i do this?

Answer

Karl Laurentius Roos picture Karl Laurentius Roos · Apr 29, 2011

Put this in a file named .htaccess in your WWW-root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php

This works if you're running Apache and have mod_rewrite activated.