Home > front end >  How do I combine these two .htaccess rules?
How do I combine these two .htaccess rules?

Time:09-30

I want to combine these two rules, but not sure how

RewriteRule ^([^\.] )$ $1.html [L] 
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

when I put both, I get the error "too many redirects"

My goal here is to combine them both,

the first rule is to remove file extensions (ex. html)

the second rule is: make every URL go to https://www.example.com, rather than https://example.com

CodePudding user response:

With the additional information you now gave I would suggest this approach:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,END]

RewriteRule ^/?(. )\.html$ /$1 [R=301,END]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END] 

In general it is a good diea to start using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues ...

  • Related