0

How to remove .html extension from the URL using .htaccess (but itā€™s challenging)

Hereā€™s the file structure. - index.html - x (A FOLDER) - im_in_x.html - me_too.html - y (FOLDER) - y_no.html - y_yes.html - some-page.html - x.html - y.html - Styles (FOLDER) - images (FOLDER) So what Iā€™m trying to do is remove the .html extension from the url using .htaccess file (and yes I have access to that file). The challenge is that I have the same folder name as the file name.

10th Mar 2021, 2:36 AM
Ginfio
Ginfio - avatar
8 Answers
+ 2
assuming you're using mod_rewrite, accessing folder or file will be quite different: access to folder ends with a slash, while access to file do not. so you need a regex url rewrite rule such as: ^(.*)(?<!\/)$ and as replacement pattern: $1.html let me know if that solve your problem, because I've never wrote url rewrite rules, i only have looking how that could be done ;P
10th Mar 2021, 3:15 AM
visph
visph - avatar
+ 1
no, use my patterns inside Calvin prefix/postfix: RewriteRule ^(.*)(?<!\/)$ $1.html [L,R=301] where there's 4 parts separated by space: + 'RewriteRule' keyword + regex to select urls to be rewritten + the replacement pattern ($1 refer to first capture group) + postfix (doesn't remember for what is L, but seems important, and R=301 redirection seems optional, but sounds like to be good practice?)
10th Mar 2021, 3:19 PM
visph
visph - avatar
+ 1
visph that one disnt work. ā€œThis page isnā€™t working www.MYDOMAIN.com redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTSā€ Hereā€™s what i used: RewriteEngine On RewriteRule ^(.*)(?<!\/)$ $1.html [L,R=301]
10th Mar 2021, 3:57 PM
Ginfio
Ginfio - avatar
+ 1
as I was saying in my first post, I never had used url rewriting, so I don't know exactly how it should be done... you could try to remove the escape anti-slash (I think we need it in javascript because we enclose regex literal inside slashes, so we need a way to differenciate the closing one from those used in the regex)... you could also try to remove ",R=301", and at least do your own research on how to write url rewriting rules ^^ and you finally should try all combinations ;P
10th Mar 2021, 4:03 PM
visph
visph - avatar
+ 1
visph I will probably just add an underscore on the folder name: (_x)... example.com/x points to the file name. example.com/_x to the folder. Hey, thanks for your time. really appriciate it. I will do some more research about this, & let you know if I figure it our somehow.
10th Mar 2021, 4:34 PM
Ginfio
Ginfio - avatar
0
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
10th Mar 2021, 11:48 AM
CalviÕ²
CalviÕ² - avatar
0
CalviÕ² didn't your solution do the converse that what the OP expect? url: "domain.tld/x" should return "x.html" file? (as if url was rewrite to "domain.tld/x.html"?) while your solution will change "domain.tld/x.html" to "domain.tld/x" (and then will return folder instead of expected file?)
10th Mar 2021, 2:40 PM
visph
visph - avatar
0
visph So would the full code be: RewriteEngine On ^(.*)(?<!\/)$1.html
10th Mar 2021, 3:12 PM
Ginfio
Ginfio - avatar