How to hide the PHP extension from the URLs?

In this tutorial I want to explain about hiding .php extension with URL rewriting with htaccess.

What is .htaccess

An .htaccess file is a simple ASCII file and it can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer.

With an .htaccess file we can do following:

  • Redirect the user to different page
  • Password protect a specific directory
  • Block users by IP
  • Preventing hot linking of your images
  • Rewrite URIs
  • Specify your own Error Documents
  • Disable/Enable built-in PHP functions

Hide .php extension

Open .htaccess(create new one if not exists) file from root of your website, and add the following code.

#Options -MultiViews
RewriteEngine on

RewriteRule ^(.*)\$ $1.php 

The above lines will help you to access URLs from example.com/index.php to example.com/index

If you want to remove other extension just replace php to your extension.

We can also change the .xxx extensions to .yyy extensions for example if you want to change .php to .html just add the following lines to .htaccess file.

#Options -MultiViews
RewriteEngine on

RewriteRule ^(.*)\.html$ $1.php 

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status