Search code examples
apache.htaccesshttp-redirectmod-rewrite

How do I combine these two .htaccess rules?


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


Solution

  • 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 ...