Search code examples
apachemod-rewritesslno-www

force SSL and no WWW on Apache


I'm having trouble coming up with the proper syntax to accomplish both forcing the SSL and no WWW.

EDIT

I've been able to accomplish each task separately but when combining the two i find myself stuck in a redirection loop.

working syntax to force no WWW:

RewriteCond %{HTTP_HOST} !^domain\.com$
RewriteRule (.*) http://domain.com/$1 [R=301,L]

My attempt to force no WWW and SSL

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} !^domain\.com$
RewriteRule (.*) https://domain.com/$1 [R=301,L]

Thanks for any suggestions!


Solution

  • For SSL you could use something like:

    Redirect / https://domain.com/
    

    Place this only in the section of your virtual host you configure for HTTP, not HTTPS, to not run clients into endless loops.