Beau Townsend
Beau Townsend

Reputation: 333

Redirect all to https://www.example.com

OK, so I know that nearly every variety of this question has been asked and answered on this forum, and many others, but I just can't seem to get it right. My hope is that if I provide enough specifics, including the process I'm following, that someone will be able to spot my error and correct me. Here goes!

The Specifics

What I want to accomplish vs what I have accomplished

I want to direct all http and https traffic to https://www.example.com. The following examples marked with a + have been accomplished; the one marked with - eludes me:

What I'm doing

Each site has a vhost file generated by Aegir, and each of these vhost files contains two VirtualHost directives:

I've modified example.com's vhost file to include the following code under the <VirtualHost *:80> directive, which has successfully achieved the three working examples above:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule (.*) https://www.example.com$1 [R=301,L]

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

The Problem

What I can't seem to figure out is how to enact the https://example.com -> https://www.example.com rewrite. I've tried many examples found on the Internet under both the <VirtualHost 0.0.0.0:443> and <VirtualHost *:80> directives with no luck.

Also, I'm reloading Apache and clearing my browser's cache after each edit of the vhost file. Any insight, guidance or correction would be much appreciated.

Thanks to all!

Edit: Below is a redacted version of my vhost file:

<VirtualHost 0.0.0.0:443>

    DocumentRoot /var/www/drupal 

    ServerName example
    SetEnv db_type  mysql
    SetEnv db_name  example
    SetEnv db_user  example
    SetEnv db_passwd  1234567
    SetEnv db_host  somedb
    SetEnv db_port  1234
    # Enable SSL handling.

    SSLEngine on

    SSLCertificateFile /ssl.d/example/example.crt
    SSLCertificateKeyFile /ssl.d/example/example.key
    SSLCertificateChainFile /ssl.d/example/example_chain.crt

  ServerAlias example.com
  ServerAlias www.example.com

<IfModule mod_rewrite.c>
  RewriteEngine on


    # this isn't working; neither has anything else that I've tried here.
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

  # Extra configuration from modules:

      # Error handler for Drupal > 4.6.7
      <Directory "/var/www/drupal/sites/example/files">
        <Files *>
          SetHandler This_is_a_Drupal_security_line_do_not_remove
        </Files>
        Options None
        Options +FollowSymLinks

        # If we know how to do it safely, disable the PHP engine entirely.
        <IfModule mod_php5.c>
          php_flag engine off
        </IfModule>
      </Directory>

    # Prevent direct reading of files in the private dir.
    # This is for Drupal7 compatibility, which would normally drop
    # a .htaccess in those directories, but we explicitly ignore those
    <Directory "/var/www/drupal/sites/example/private/" >
      <Files *>
        SetHandler This_is_a_Drupal_security_line_do_not_remove
      </Files>
      Deny from all
      Options None
      Options +FollowSymLinks

      # If we know how to do it safely, disable the PHP engine entirely.
      <IfModule mod_php5.c>
        php_flag engine off
      </IfModule>
    </Directory>

  </VirtualHost>

<VirtualHost *:80>

  DocumentRoot /var/www/drupal
    ServerName example
    SetEnv db_type  mysql
    SetEnv db_name  example
    SetEnv db_user  example
    SetEnv db_passwd  1234567
    SetEnv db_host  somedb
    SetEnv db_port  1234

  ServerAlias example.com
  ServerAlias www.example.com

<IfModule mod_rewrite.c>

  # these rules work for everything except:
  #  https://example.com -> https://www.example.com
  RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  RewriteRule (.*) https://www.example.com$1 [R=301,L]

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

</IfModule>

# Extra configuration from modules:

    # Error handler for Drupal > 4.6.7
    <Directory "/var/www/drupal/sites/example/files">
      <Files *>
        SetHandler This_is_a_Drupal_security_line_do_not_remove
      </Files>
      Options None
      Options +FollowSymLinks

      # If we know how to do it safely, disable the PHP engine entirely.
      <IfModule mod_php5.c>
        php_flag engine off
      </IfModule>
    </Directory>

    # Prevent direct reading of files in the private dir.
    # This is for Drupal7 compatibility, which would normally drop
    # a .htaccess in those directories, but we explicitly ignore those
    <Directory "/var/www/drupal/sites/example/private/" >
      <Files *>
        SetHandler This_is_a_Drupal_security_line_do_not_remove
      </Files>
      Deny from all
      Options None
      Options +FollowSymLinks

      # If we know how to do it safely, disable the PHP engine entirely.
      <IfModule mod_php5.c>
        php_flag engine off
      </IfModule>
    </Directory>


</VirtualHost>

Upvotes: 2

Views: 2164

Answers (1)

Capsule
Capsule

Reputation: 6159

OK now I get it with the content of your virtualhost.

This is not valid:

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

Actually you can't use %{HTTP_HOST} or %{REQUEST_URI} in the RewriteRule part

That's why it's redirecting you to a non-existent host, hence the connection refused error.

You should use this rule in the ssl virtualhost:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [R=301,L]

And this one in the non ssl one:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com$1 [R=301,L]

HTH

Upvotes: 1

Related Questions