重定向所有https://www.example.com [英] Redirect all to https://www.example.com

查看:359
本文介绍了重定向所有https://www.example.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我知道,几乎每一个品种这一问题已被要求,并回答了在这个论坛上,和许多其他人,但我似乎无法得到它的权利。我的希望是,如果我提供足够的细节,其中包括我在下面的过程中,会有人能够发现我的错误和指正。这里去!

的细节


  • Drupal的多站点

  • Apache Web服务器

  • 网站由Aegir置备

我要完成什么VS什么,我已经完成了

我想将所有HTTP和HTTPS流量,以 https://www.example.com 。标有下面的例子 + 已经完成;一个标有 - 躲开我:

我在做什么

每个站点都有所Aegir生成的虚拟主机文件,并且每个虚拟主机文件包含两个VirtualHost指令:


  • <虚拟主机0.0.0.0:443>

  • <虚拟主机*:80>

我修改 example.com 的虚拟主机文件包含在&LT以下code;虚拟主机*:80> 指令,它已成功地实现上述三个工作的例子:

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    的RewriteCond%{HTTP_HOST} ^例如\\ $ .COM [NC]
    重写规则HTTPS(*)://www.example.com$1 [R = 301,L]    的RewriteCond%{} HTTPS关闭
    重写规则HTTPS(。*)://%{HTTP_HOST}%{REQUEST_URI}
< / IfModule>

问题

我似乎无法弄清楚是如何制定的 https://example.com - > < A HREF =htt​​ps://www.example.com相对=nofollow> https://www.example.com 重写。我都试过了℃在互联网上找到许多例子;虚拟主机0.0.0.0:443> &LT;虚拟主机*:80&GT; 没有运气指令。

另外,我重装Apache和虚拟主机文件的每个编辑后清除浏览器的缓存。任何见解,指导或纠正会大大AP preciated。

感谢所有!

编辑:
下面是我的虚拟主机文件的删节版:

 &LT;虚拟主机0.0.0.0:443>    DocumentRoot的/无功/网络/的Drupal    例如服务器名
    SETENV db_type的mysql
    SETENV例如DB_NAME
    SETENV例如DB_USER
    SETENV db_passwd 1234567
    SETENV DB_HOST somedb
    SETENV DB_PORT 1234
    #启用SSL处理。    在SSLEngine的    了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&LT; IfModule mod_rewrite.c&GT;
  RewriteEngine叙述上
    #这是不工作;既没有其他任何东西,我在这里尝试。
    的RewriteCond%{HTTP_HOST} ^!WWW \\。
    重写规则^ $ HTTPS(*)://www.% {} HTTP_HOST%{REQUEST_URI} [L,R = 301]&LT; / IfModule&GT;  从模块#额外的配置:      为Drupal&GT#错误处理程序; 4.6.7
      &LT;目录/无功/网络/的Drupal /网站/例子/文件&GT;
        &LT;文件*&GT;
          SetHandler This_is_a_Drupal_security_line_do_not_remove
        &LT; /文件&GT;
        选择无
        选择了FollowSymLinks +        #如果我们知道如何做到这一点安全,完全禁用PHP引擎。
        &LT; IfModule mod_php5.c&GT;
          的php_flag停机
        &LT; / IfModule&GT;
      &LT; /目录&GT;    #prevent私人DIR文件的直接读取。
    #这是Drupal7的兼容性,这通常会下降
    #在这些目录中的.htaccess,但我们明确地忽略这些
    &LT;目录/无功/网络/的Drupal /网站/例子/私营/&GT;
      &LT;文件*&GT;
        SetHandler This_is_a_Drupal_security_line_do_not_remove
      &LT; /文件&GT;
      所有拒绝
      选择无
      选择了FollowSymLinks +      #如果我们知道如何做到这一点安全,完全禁用PHP引擎。
      &LT; IfModule mod_php5.c&GT;
        的php_flag停机
      &LT; / IfModule&GT;
    &LT; /目录&GT;  &LT; /虚拟主机&GT;&LT;虚拟主机*:80&GT;  DocumentRoot的/无功/网络/的Drupal
    例如服务器名
    SETENV db_type的mysql
    SETENV例如DB_NAME
    SETENV例如DB_USER
    SETENV db_passwd 1234567
    SETENV DB_HOST somedb
    SETENV DB_PORT 1234  ServerAlias​​ example.com
  ServerAlias​​ www.example.com&LT; IfModule mod_rewrite.c&GT;  #这些规则除一切工作:
  #https://example.com - &GT; https://www.example.com
  的RewriteCond%{HTTP_HOST} ^例如\\ $ .COM [NC]
  重写规则HTTPS(*)://www.example.com$1 [R = 301,L]  的RewriteCond%{} HTTPS关闭
  重写规则HTTPS(。*)://%{HTTP_HOST}%{REQUEST_URI}&LT; / IfModule&GT;从模块#额外的配置:    为Drupal&GT#错误处理程序; 4.6.7
    &LT;目录/无功/网络/的Drupal /网站/例子/文件&GT;
      &LT;文件*&GT;
        SetHandler This_is_a_Drupal_security_line_do_not_remove
      &LT; /文件&GT;
      选择无
      选择了FollowSymLinks +      #如果我们知道如何做到这一点安全,完全禁用PHP引擎。
      &LT; IfModule mod_php5.c&GT;
        的php_flag停机
      &LT; / IfModule&GT;
    &LT; /目录&GT;    #prevent私人DIR文件的直接读取。
    #这是Drupal7的兼容性,这通常会下降
    #在这些目录中的.htaccess,但我们明确地忽略这些
    &LT;目录/无功/网络/的Drupal /网站/例子/私营/&GT;
      &LT;文件*&GT;
        SetHandler This_is_a_Drupal_security_line_do_not_remove
      &LT; /文件&GT;
      所有拒绝
      选择无
      选择了FollowSymLinks +      #如果我们知道如何做到这一点安全,完全禁用PHP引擎。
      &LT; IfModule mod_php5.c&GT;
        的php_flag停机
      &LT; / IfModule&GT;
    &LT; /目录&GT;
&LT; /虚拟主机&GT;


解决方案

OK,现在我与你的虚拟主机的内容得到它。

这是无效的:

 的RewriteCond%{HTTP_HOST} ^!WWW \\。
重写规则^ $ HTTPS(*)://www.% {} HTTP_HOST%{REQUEST_URI} [L,R = 301]

其实你不能使用%{HTTP_HOST} %{REQUEST_URI} 重写规则部分

这就是为什么它重定向你到一个不存在的主机,因此拒绝连接错误。

您应该在SSL虚拟主机使用这样的规则:

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST} ^例如\\ $ .COM [NC]
重写规则HTTPS(*)://www.example.com$1 [R = 301,L]

而这一次在非SSL之一:

  RewriteEngine叙述上
的RewriteCond%{} HTTPS关闭
重写规则HTTPS(*)://www.example.com$1 [R = 301,L]

心连心

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

  • Drupal multi-site
  • Apache web server
  • Sites provisioned by Aegir

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:

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

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>

解决方案

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

这篇关于重定向所有https://www.example.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆