将所有内容重定向到https://www.example.com

时间:2015-09-10 02:44:01

标签: apache mod-rewrite vhosts

好的,所以我知道这个问题的几乎所有问题都已经在这个论坛和其他许多问题上得到了回答,但我似乎无法做到这一点。我的希望是,如果我提供足够的细节,包括我正在遵循的过程,那么有人将能够发现我的错误并纠正我。好了!

具体细节

  • Drupal多站点
  • Apache Web服务器
  • Aegir提供的网站

我想要完成的事情与我所取得的成就

我想将所有http和https流量定向到https://www.example.com。标有+的以下示例已经完成;标有-的那个包括我:

我正在做什么

每个站点都有一个由Aegir生成的vhost文件,每个vhost文件都包含两个VirtualHost指令:

  • <VirtualHost 0.0.0.0:443>
  • <VirtualHost *:80>

我修改了example.com的vhost文件,在<VirtualHost *:80>指令下包含以下代码,该指令已成功实现上述三个工作示例:

<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>

问题

我似乎无法弄清楚如何制定https://example.com - &gt; https://www.example.com重写。我已尝试在互联网上根据<VirtualHost 0.0.0.0:443><VirtualHost *:80>指令找到的许多例子没有运气。

此外,我在重新加载Apache并在每次编辑vhost文件后清除浏览器的缓存。任何见解,指导或纠正都将非常感激。

感谢所有人!

编辑: 以下是我的vhost文件的编辑版本:

<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>

0 个答案:

没有答案