Rewrite help

Discussion in 'Install/Configuration' started by PSS, May 30, 2018.

  1. PSS

    PSS Well-Known Member

    I have now a www -> non-www redirect running:
    Code:
    RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
    this has worked well.
    --------

    Now I have SSL on, and need to 301 redirect all http requests to matching https content.
    Also, ://www.mydomain.com to ://mydomain.com

    I suck at rewrite code.

    Web is full of solutions, and they are almost all different. What is your proposal? I really can not start testing these on a live site without being absolutely sure they work.

    This looks most sane (from https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/):
    Code:
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^ https://mydomain.com%{REQUEST_URI} [L,NE,R=301]
    Is there any difference to this:
    Code:
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ https://mydomain.com/$1} [R=301,L]

    or, is there a better solution. Google absolutely requires a 301 redirect (so that they can simply update their index and not reindex the site), so I hope for a solution that does it cleanly. I have certificates working for both www and non-www.

    Thanks a lot.
     
    Last edited by a moderator: May 31, 2018
  2. NiteWave

    NiteWave Administrator

    2nd one looks best to me. but looks there is an extra } in it ? should be
    Code:
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
    
     
  3. PSS

    PSS Well-Known Member

    Thanks.

    That works but not totally:

    https://www.domain.com
    still stays at
    https://www.domain.com

    Gives "200 OK". i.e www. is not removed (I need to have it removed).

    BTW: http://www.redirect-checker.org/index.php seems to be a good page to show the redirect headers.

    Any ideas?

    Cat I chain these like this:
    Code:
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]


    EDIT: yes, that seems to do it properly :)
     
    Last edited by a moderator: May 31, 2018

Share This Page