.htaccess file redirecting http://www.domain.com to https://domain.com

Discussion in 'Webmastering & Programming' started by Flea77, Sep 21, 2018.

  1. Flea77

    Flea77 Limp Gawd

    Messages:
    142
    Joined:
    Nov 9, 2004
    So, I know virtually noting about the .htacess file other than some very basic ideas of what it is used for. Mine is huge (142 lines), I think primarily for the WordPress cache plugin I use, which makes it all that more confusing. But I digress.

    My current site will redirect http://www.domain.com to https://domain.com and that seems to be a problem with my CDN as it wants everything to be on a www.domain.com.

    So what I think I need is to change it from what it is doing now to redirecting to https://www.domain.com. I believe the lines in the file responsible for this are:

    RewriteCond %{HTTPS} =on
    RewriteCond %{HTTP_HOST} ^domain.com

    but I am unsure of how to modify them to achieve what I need. Those two lines appear in two different places in the .htaccess file. There is also these lines which may mean something:

    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    and lastly, I may need to modify this line:

    RewriteCond %{HTTP_HOST} ^domain.com

    Which appears by itself (without the RewriteCond %{HTTPS} =on line above it).

    Ideas? Suggestions? Pointers?
     
  2. Snowknight26

    Snowknight26 [H]ardness Supreme

    Messages:
    4,143
    Joined:
    May 8, 2005
    Code:
    RewriteEngine On
    # Redirect to the correct domain name
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
    RewriteRule ^/?(.*)$ https://www.domain.com/$1 [NE,L,R=301]
    
    Should do it. But realistically you should modify your Apache .conf instead of the .htaccess since that has to be parsed each hit.