• Resolved johnc1979

    (@johnc1979)


    Hi everyone,

    I hope someone can help me and I’m not in the wrong place.

    I recently moved hosts and my old host had the default domian as https://www. Since moving to Godaddy it’s been switched to https. I have redirects in the .htaccess file to point to the non www version, but whenever someone visits the https://www version the site is broken and doesn’t load the css. The weird thing is if you refresh the page it loads correctly.

    I’ve switched the preferred domain in Webmaster tools to the https version, but the https:///www. is still causing problems.

    At the top of the .htaccess I have this

    RewriteEngine On
    
    # match any URL with www and rewrite it to https without the www
    RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
    RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
    
    # match urls that are non https (without the www)
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    What am I doing wrong? It works, but only on a refresh. I don’t want to drive customers away with an ugly broken site.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try replacing your rewrite code with this.

    # Redirect Non-WWW to WWW
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
    
    # Redirect HTTP to HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule .* https://www.yoursite.com%{REQUEST_URI} [R,L]
    Thread Starter johnc1979

    (@johnc1979)

    Thanks for the snippet Davood Dehnavi, but it didn’t work. ??

    It looks like the snippet you gave me is trying to direct to https://www.mysite.com where I want to direct all to https://mysite.com

    This is what I want

    www >>https://mysite.com
    https://www >>https://mysite.com
    https:// >>https://mysite.com
    https://www >>https://mysite.com

    I’ve currently got everything but https://www >>https://mysite.com working

    The original snippet I used above works, but never on the first load for the https://www. It only works when the page is refreshed

    Hello johnc1979,

    Please try the following code in your .htaccess file and remove your code:

    RewriteEngine On
    
    # Check that you're on port 443 and the hostname starts with www
    RewriteCond %{SERVER_PORT} ^443
    RewriteCond %{HTTP_HOST} ^www\.
    
    # Redirect to domain without the www
    RewriteRule (.*) https://yoursite.com$1 [L,R,QSA]

    @johnc1979, I misunderstood what you were trying to do… You can use the code that was provided by @cedcommerce or you can change the code I provided to this

    # Force url without WWW
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^yoursite.com$ [NC]
    RewriteRule ^(.*)$ https://yoursite.com/$1 [L,R=301]
    
    # Force https
    RewriteCond %{HTTP_HOST} ^yoursite\.com [NC]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://yoursite.com/$1 [R,L]

    Thread Starter johnc1979

    (@johnc1979)

    It all seems to be good now. I just tried loading it again using the top code and it redirected the first time perfectly. Not sure why it wasn’t working that way to begin with, but it seems ok now.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘force from https://www to https’ is closed to new replies.