Apply two functions to . htaccess

Asked

Viewed 57 times

1

I don’t quite understand the . htacces, but I use it to force the navigation of my site to the https, now I’m trying to implement a code to remove the .html but when I put the two together it doesn’t work.

Force https:

RewriteEngine on
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R] 

Remove . html from URL:

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

How I make the two of you work together?

1 answer

1


When Rewriterule matches, the flag [L] (LAST) stops processing other rules.
You have to remove it from the first rule.

RewriteEngine On

#Forçar https
RewriteCond     %{SERVER_PORT}      ^80$
RewriteRule     ^                   https://%{SERVER_NAME}%{REQUEST_URI} [R]

#Retirar .html da URL
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule     ^(.*)\.html$        $1        [NC,R=301]

And, after redirecting, if you want to rewrite all requests without .html to the page with .html, can add:

#Reescrever o .html (adicionado internamente, o usuário não o vê)    
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule     ^((?:[^.]*|.*[^.]{5})[./])$   $1.html
  • But the ". html" still continues even taking the [L] of the top.

  • What do you mean it doesn’t exist? I thought it would take the ". html" out of the URL I had done something like this, must have fooled me then, sorry.

  • @Bretone RewriteCond %{REQUEST_FILENAME} !-f sets a condition to run the rule only when the file (-f) is not present in the folder. You can remove this condition to check if the rule is working, but I think this will give you a 404.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.