0

I want to hide .html extension in URL, which I did by editing .htaccess file. If I click on any page then it opens without .html extension but when I manually enter url with .html extension it also opens the page and .html extension appears in the URL.

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

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php71” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php71 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

My requirement is to not allow a page to open up even if I enter .html in the url. Or it should automatically redirect when user enters URL with .html extension.

I don't know if the above code is correct. Please help me to fix it. Website: https://drkrinitamotwani.com/

Kashiii
  • 37
  • 2
  • 3
  • 13

1 Answers1

1

I played around for a while and got the following .htaccess config to work:

This will remove the .html from URLs and also make sure to load the .html files when the URL is visited without the .html

You can test it here: http://martinsbh.com.au/test.html

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

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

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php71” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php71 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
cam
  • 3,179
  • 1
  • 12
  • 15
  • I used this before like this but it didn't work : RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html [NC,L] – Kashiii Jun 09 '20 at 06:05
  • That is different to what I posted – cam Jun 09 '20 at 06:06
  • Also I want to force redirect if user enters URL with .html extension. It should redirect to URL without .html extension – Kashiii Jun 09 '20 at 06:06
  • This should do that – cam Jun 09 '20 at 06:07
  • Tried that aswell. But my question is to force the URL if user enters .html extension in the URL – Kashiii Jun 09 '20 at 06:15
  • You are right, it didnt work. I've updated my answer, try the new one! – cam Jun 09 '20 at 06:25
  • I have added your code but please check my website, it still opens up with .html not redirecting : https://drkrinitamotwani.com/events.html – Kashiii Jun 09 '20 at 06:37
  • I think it is working in incognito. Does it require time for changes to work ? – Kashiii Jun 09 '20 at 06:40
  • That link is working for me both in and out of incognito too. Potentially your browser is caching an old redirect/rewrite rule. – cam Jun 09 '20 at 06:45
  • Thanks for help buddy. I am marking your answer as accepted :) – Kashiii Jun 10 '20 at 01:18
  • One last question if you can help with please. How can I redirect this URL: https://drkrinitamotwani.com/index to this one https://drkrinitamotwani.com – Kashiii Jun 10 '20 at 01:20
  • 1
    Add this line "Redirect 301 /index /" without the quotation marks, between the second and third block, line 7. – cam Jun 10 '20 at 01:25
  • can you please edit the code and add it ? – Kashiii Jun 10 '20 at 01:27
  • Yes its working thanks for the help :) – Kashiii Jun 10 '20 at 01:33