Jason Brody
Jason Brody

Reputation: 368

removing .html URL extension using .htaccess not working

I'm using the below code to remove .html extention form url, but it is not working..

can any one help , where i have to put the .htaccess file

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html

Upvotes: 0

Views: 2325

Answers (4)

Tabassam Ali
Tabassam Ali

Reputation: 262

I would like to share an understandable, simple way to remove the .html extension from the URL using the .htaccess file. I'm using XAMPP on Windows 10. For me, it's working perfectly. If you want the same result, just follow the steps given below...

For removing the .html extension, e.g. if you want to change mywebsite.com/contact.html to mywebsite.com/contact you have to add the following code inside your .htaccess file:

First of all, create a new file named .htaccess and placed it at your root-directory where your index file is placed. Now, open this (.htaccess) file with any editor of your choice and write/copy/paste the given code in your file and save it...

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]

Now, if you (any user) will access mywebsite.com/contact in the browser, it will show the content of mywebsite.com/contact.html page.

So, what if any user will access the URL as mywebsite.com/contact.html? this will redirect the user to this mywebsite.com/contact.html page instead of this mywebsite.com/contact which means the user can still visit the mywebsite.com/contact.html page. Don't worry about it. If you want to avoid this, the next step is for you...

To avoid the above problem and for redirecting users to the newly created URL, you need to add some more rules in your .htaccess file. So, open it again and replace your old code with the new one given below and save your file...(You can ignore this step, It's on you...)

RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

Now, you're all set. I hope this will fix everyone's problem.

Have a nice day :)

Upvotes: 3

Jason Brody
Jason Brody

Reputation: 368

Finally I Got the correct .HTACCESS code to remove .php extention... :)

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Upvotes: 1

anubhava
anubhava

Reputation: 784898

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Upvotes: 2

Sunil Pachlangia
Sunil Pachlangia

Reputation: 2071

Use this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

Upvotes: 0

Related Questions