February 4, 2020 0 Comments

Last Updated on April 15, 2021 by amitoverflow1

Remove php and html file extension from URLs using RewriteRule in htaccess

In this post we will learn how to use RewriteRule directive to remove html and php file extensions from URLs.

I know there are thousands of similar posts already indexed on the web but this post is unique and helpful to URL Rewriting beginners. I will provide you a 100% working code and you will learn to write your own code from the explanations.

Removing file extension from URLs is easy. You don’t need to be a URL rewriting expert to do that. With just two lines of RewriteRule code you can enable extension-less file requests on your server.
URLs without file extension look clean and preety. I personally use mod-rewite to hide file extensions from URLs because It makes my file URL short and easier to remember.

hide .html extension with htaccess

A basic rule that will allow you to visit html files without using .html extension at the end.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*)/?$ /$1.html [L]

With the above rule you can access your file url as


Without typing the traling .html.

This rule doesn’t remove .html extension from URLs . It just makes it so that you can access your html file URLs without adding the extension at the end of the file name.
The rule rewrites your extension-less file requests to the orignal file location .Ie: /file will get rewritten to /file.php . The rewriting happens behind the scene and not visible to browser.

The drawback of using above rule is that it leaves you with two URLs pointing to the same location which can result in duplicate contents issues on search engines.
As I alrealdy mentioned, this rule doesn’t remove .html , using this simple rule you can access your html files either by going to /file or /file.php . Search engines can crawl both versions of your URLs .

To avoid this , you need to 301 redirect your .html URL to the new format ie: /file.html => /file . To permanently redirect your orignal URLs to the new format use a new rule. See the “complete rule to hide .html extension” bellow.

Remove .html extension from URLs (The complete rule)

Here is the complete rule that will remove html from URLs. You can use this in htaccess in /public_html folder .

RewriteEngine on
#1) redirect file.html to file
RewriteCond %{THE_REQUEST} /([^.]+).html [NC]
RewriteRule .+ /%1 [L,R=301]
#2) rewrite /file to file.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*)/?$ /$1.html [L]

This rule will change your html file URL from




The rule will 301 redirect your html file URLs to its extension-less version taking care of the SEO rank of your domain URLs.

Remove .php extension from URLs (The complete rule)

If you need to remove .php from URLs, you can use the following RewriteRule in htaccess in your site root folder :

RewriteEngine on
#1) redirect file.php to file
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule .+ /%1 [L,R=301]
#2) rewrite /file to file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)/?$ /$1.php [L]

Leave a Reply

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