Hi! This post will show you how to remove .php and .html extensions from url using htaccess file.  Having file extensions like .php, .html at the end of an url does not hurt seo but removing them will make the url more readable.  CMS like Wordpress hides file extensions in url which makes it user friendly.  You can do the same to other websites (even the one built with core PHP) with the help of .htaccess file.
An .htaccess File is an ASCII file which allows you to set configurations of the Apache Server. With htaccess file, you can rewrite URIs, block IPs, restrict access to directory with password etc. Here we'll see how to remove/hide the file extensions from url with htaccess file.
Removing .php Extension from URL
Go to the website root and open the .htaccess file in the notepad or similar editor.  If you can't find the file then create one.  Now add the following code to the htaccess file and save.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
- The first line 
RewriteEngine onturns on the apache rewriting engine. This is mandatory for rewriting urls. If omitted, the rewriting rules will be ignored. - The next line 
RewriteCond %{REQUEST_FILENAME}.php -fasks to look for a file with.phpextension. - The final line 
RewriteRule ^(.*)$ $1.phpwill rewrite the url to remove.phpextension from the file url. 
After saving the htaccess file, restart apache and you can access the website urls without extension now.
Say you have a website url,
www.example.com/hiking-backpacks.php
Then you can access it without the file extension like this,
www.example.com/hiking-backpacks
This applies to all of the urls of the website that ends with .php.
Removing .html Extension from URL
Similarly you can remove the .html extension from the url too.  Just add these below lines to the htaccess file and save.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
Note: Make sure to restart apache after saving the file for the changes to take effect.
On the whole this is how your htaccess file should look like if you want to remove both php and html extensions in the website url.
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
The above code will let you access the website urls without php or html extensions making it more readable.
Likewise you can easily remove .php & .html extensions from url using htaccess file.
Hai ,
ReplyDeleteI have above code for removing .php extension.Its working fine for url with / with out .php.
But I want to redirect with out .php url If I run with .php extension.
Example
If I click on below hyper link
http://www.miraclesoft.com/why/innovation.php
It should be display in address bar like below
http://www.miraclesoft.com/why/innovation
Hai ,
ReplyDeleteI have placed and published the urls with .php file extension.Now my website is working both with/with out file extension.But I want to change the url with out file extension if I access old url .