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.
You can also refer this tutorial to create seo friendly url. How to create seo friendly url using htaccess




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.

Thanks 🙂

If you like this post please don’t forget to subscribe My Public Notebook for more useful stuff.

3 Replies to “Remove or change .php and .html extension using .htaccess”

  1. Does not work.

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    # Remove .php extension from url
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^([^\.]+)/$ $1.php

    # Remove .html extension from url
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^([^\.]+)/$ $1.html

    But the below code to change .rohit extension works

  2. Thanks for letting me know i checked current code, It is working if you add slash at the end of the url like http://www.example.com/user/, So i just fixed the issue and updated the post use below code to remove .php and .html extension without slash

    <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>
  3. hello sir,
    i created a plugin for my site
    and in my site there is a plugin named “.html on pages” is activated
    now the problem is when i activate my plugin .html extension applied in my url. and because of that the plugin is not working.
    how can i apply your rewrite code inly in my plugin…
    and yes my url is like this “http://localhost/ajay/wordpress/projecttask/?p=project_detail.html&p_id=94836894-0813-6b91”

    how can i change “/?p=project_detail.html&” to “/?p=project_detail&”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.