Remove or change .php and .html extension using .htaccess

If you want to remove .html or .php extension from the url, Or want to change .html or .php extension with any custom extension then this tutorial will help you lot to get solution.

If you need SEO friendly URL for your web based application then it is important to remove special characters from the url.

So first of all create .htaccess file and placed it to your root project directory & add some command to check whether rewrite engine on or not.

.htaccess

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase / 

 </IfModule>

Note: By default this file in hidden format please change your system settings and display hidden files to view it.
Note: Before executing this file please make sure that you have successfully enabled mod_rewrite extension in php.ini file

Now your basic .htaccess file has been ready, Next write code to remove .html and .php extension from the URL.

Before applying .htaccess rule url will be.
http://www.example.com/user.php
http://www.example.com/user.html

After applying .htaccess rule url will be.
http://www.example.com/user

Here is the complete .htaccess code to remove .html and .php extension form the url.

.htaccess

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /
 
# Remove .php extension from url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^([^\.]+)$ $1.php [NC,L] 
 
# Remove .html extension from url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^([^\.]+)$ $1.html [NC,L] 
 
 </IfModule>




Add slash at the end of the url like
http://www.example.com/user/
Then your htaccess file will be..

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /
 
# Add slash at end of the url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]
 
# Remove .php extension from url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^([^\.]+)$ $1.php [NC,L]
 
# Remove .html extension from url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^([^\.]+)$ $1.html [NC,L] 
 
 </IfModule>

Change .php and .html extension with custom extension

If you want to change default .html or .php extension with any custom extension then put below code in your .htaccess file.

Here i am going to remove .php extension with my name (.rohit).

Before applying .htaccess rule url will be.
http://www.example.com/user.php
http://www.example.com/user.html

After applying .htaccess rule url will be.
http://www.example.com/user.rohit

.htaccess

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /
 
# Chnage .php extension to .rohit
  RewriteCond %{THE_REQUEST} (.*)\.php  
  RewriteRule ^(.*)\.php $1.rohit [R=301,L]  
 
# Chnage .html extension to .rohit
  RewriteCond %{THE_REQUEST} (.*)\.html  
  RewriteRule ^(.*)\.html $1.rohit [R=301,L]  
 
 </IfModule>

You can use any custom extension name, just replace .rohit to your custom name from the above code snippet.

Hope this “Remove or change .php and .html extension using .htaccess” tutorial will help you to understand htaccess power.

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

PAGE TOP
error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email