简体   繁体   English

全部重定向到 https://www.example.com

[英]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细节

  • Drupal multi-site Drupal 多站点
  • Apache web server阿帕奇网络服务器
  • Sites provisioned by Aegir 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 .我想将所有 http 和 https 流量定向到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:每个站点都有一个由 Aegir 生成的 vhost 文件,每个 vhost 文件都包含两个 VirtualHost 指令:

  • <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:我修改了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>

The Problem问题

What I can't seem to figure out is how to enact the https://example.com -> https://www.example.com rewrite.我似乎无法弄清楚如何制定https://example.com -> https://www.example.com重写。 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.我已经尝试了许多在 Internet 上找到的<VirtualHost 0.0.0.0:443><VirtualHost *:80>指令下的示例,但都没有成功。

Also, I'm reloading Apache and clearing my browser's cache after each edit of the vhost file.此外,每次编辑 vhost 文件后,我都会重新加载 Apache 并清除浏览器的缓存。 Any insight, guidance or correction would be much appreciated.任何见解、指导或纠正将不胜感激。

Thanks to all!谢谢大家!

Edit: Below is a redacted version of my vhost file:编辑:以下是我的 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>

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实际上你不能在RewriteRule部分使用%{HTTP_HOST}%{REQUEST_URI}

That's why it's redirecting you to a non-existent host, hence the connection refused error.这就是为什么它将您重定向到一个不存在的主机,因此connection refused错误。

You should use this rule in the ssl virtualhost:您应该在 ssl 虚拟主机中使用此规则:

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:这是非 ssl 中的一个:

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

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将所有流量重定向到https://www.example.com - Redirect all traffic to https://www.example.com htaccess重定向https:// www。 (https://www.example.com)到非www(https://example.com) - htaccess redirect https://www. (https://www.example.com) to non www (https://example.com) 将所有URL请求重写为https://www.example.com/$1 - Rewrite all URL requests to https://www.example.com/$1 Apache2 使用虚拟主机将 https://example.com 重定向到 https://www.example.com - Apache2 redirect https://example.com to https://www.example.com with vhosts 如何将https://example.com/重定向到https://www.example.com/ - How to Redirect https://example.com/ to https://www.example.com/ 如何将 https://www.example.com/aaa 重定向到 https://example.com/aaa/ - how to redirect https://www.example.com/aaa to https://example.com/aaa/ www.example.com没有重定向到HTTPS - www.example.com not redirecting to HTTPS http://example.com不会重定向到https://www.example.com - http://example.com would not redirect to https://www.example.com 解析 http://www.example.com 以通过 htaccess 永久重定向到 https://example.com - Resolving http://www.example.com to permanently redirect to https://example.com via htaccess 如何将 http://www.example.com 重定向到 https://example.Z4D236D9A2D102C5ZFE6AD1Cware - How to redirect http://www.example.com to https://example.com with middleware
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM