0

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 
5
  • RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.html [NC,L] Commented Apr 14, 2015 at 13:03
  • 1
    What is the URL you're attempting? Currently that will only match up to the first /, so if you're attempting/this/file the rewrite will try attempting.html Commented Apr 14, 2015 at 13:13
  • Have you restart your server Commented Apr 14, 2015 at 13:17
  • @arco444 I'm using only www.sitename.com/index.html Commented Apr 14, 2015 at 13:18
  • Thank you, but that is not working. where i have to place the .htaccess file. i copied that file in public_html Commented Apr 14, 2015 at 13:23

4 Answers 4

3

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

Kindly visit this answer... stackoverflow.com/a/69050563/15639663 ... if you want to know how to remove the .php extension.
2

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] 

Comments

1

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] 

1 Comment

You asked to remove .html extension in question and answering your question for removing .php extension.
0

Use this

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.