How can I remove file extension from a website address?

sumit picture sumit · Jun 30, 2011 · Viewed 98.5k times · Source

I am designing a website. I want my website address to look like the following image:

File name extensions like (PHP/JSP) are hidden

I don't want my website to look like http://something.com/profile.php. I want the .php extension to be removed in the address bar when someone opens my website. In other words, I want my website to be like: http://something.com/profile

As a second example, you can look at the Stack Overflow website address itself.

How can I get this done?

Answer

Igor picture Igor · Jun 30, 2011

Just add an .htaccess file to the root folder of your site (for example, /home/domains/domain.com/htdocs/) with the following content:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

More about how this works in these pages: mod_rewrite guide (introduction, using it), reference documentation