Hide PHP extension using .htaccess


Last time I saw a site which was done in php but the url of the site was so clean, Extension .php was not there in it . This made me think how I can do this ?

What is it ?

Hide the .php extension of your PHP files in the URL of your site address.

You site URL:

https://nithinkk.wordpress.com/2010/11/24/setting-up-virtual-box.php

Will Look like :

https://nithinkk.wordpress.com/2010/11/24/setting-up-virtual-box

Why you must hide ?

  • it looks cleaner and easier to read and remember
  • Search Engine friendly URL
  • Security
  • it makes your website environment-independent, so if you ever decide to change the technology your site uses, everything would appear seamless to your visitors.

How to hide ?

There are different ways to do this !

In this case, we’re using PHP files, but you can change it to whatever type of file you’re using, be it .html, .asp, .cfm, or anything else, as long as they’re all the same type. (If you want to do this for multiple file types, just copy lines 2–4 and apply the same technique accordingly.)

Open your text editor and create a file called “.htaccess” with the following code in it, and upload it to your site’s root directory (Note: On Unix and unix-like operating systems, files that start with a dot are hidden files, so you may not be able to see the file after you save it. To get around this, omit the preceding dot when naming the file, and then rename the file back to “.htaccess” after you have uploaded it to your webserver):

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

(adapted from the Apache mailing list)

Using this code, instead of having to type in http://nithinkk.com/contact.php, you only need to enter http://nithinkk.com/contact to access that page. And the best part is, you can still access the page with .php on the end of it, so no old incoming links or bookmarks become orphaned as a result of this, and everyone is happy.

Advertisements

2 thoughts on “Hide PHP extension using .htaccess

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: