1

Below is the code that I'm using on a .htaccess page in order to get rid of the ".html" at the end of the URL. It works fine on all pages except for one. Would any of the code on that one page be interfering with this code?

If anyone has any advice on how I'd be able to get rid of the ".html" on the URL using a MAMP server, it'd be much appreciated. Just so you know, I'm using a MAC with version 12.4 of Monterey.

Options +MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /(.*)\.html($|\ )
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
1
  • 2
    Would you care to share which URL does not work ? Commented Aug 13, 2022 at 12:07

2 Answers 2

0

This (supplied by others here) works for me

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(?!.+\.\w{2,4})(.+)$ $1.html [L]
5
  • That doesn't solve the issue. I'm getting an "Index of /" error message. Would any code in either of the files be blocking each other out?
    – Richard
    Commented Aug 13, 2022 at 7:23
  • @Richard - it`s unlikely that the contents of the file would do this. Unless you have redirects in there. Commented Aug 13, 2022 at 12:05
  • @Richard "I'm getting an "Index of /" error message" - That's not an "error message". That would imply the URL/file you are requesting (without .html) also happens to match the name of a filesystem directory. This is indeed a "problem" since mod_dir will append the trailing slash and the rewrite to append the .html file extension will fail.
    – MrWhite
    Commented May 11, 2023 at 0:18
  • @RohitGupta Aside: The regex you are using is missing a $ in the negative lookahead. It should be ^(?!.+\.\w{2,4}$)(.+)$. The point of the \w{2,4} part is to exclude requests that have a file extension, but if you exclude that first $ then you're basically excluding any request that simply contains a dot (which if that was the intention then you could write it more simply as ^(?!.+\.)(.+)$). "supplied by others here" - would you happen to still have the link? (It kinda looks like something I might have written?!)
    – MrWhite
    Commented May 11, 2023 at 0:37
  • @MrWhite, yes you did write it. Here is the Link Commented May 11, 2023 at 11:49
0

You can use the following code to remove .HTML extension from URL,

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.