【问题标题】:Redirect all to https://www.example.com全部重定向到 https://www.example.com
【发布时间】:2015-12-06 04:08:54
【问题描述】:

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

细节

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

我想要完成的 vs 我已经完成的

我想将所有 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 -> https://www.example.com 重写。我尝试了很多在 Internet 上找到的 &lt;VirtualHost 0.0.0.0:443&gt;&lt;VirtualHost *:80&gt; 指令下的示例,但没有成功。

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

谢谢大家!

编辑: 以下是我的 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>

【问题讨论】:

  • https://example.com -> https://www.example.com 应该由你的第一条规则来处理。 &lt;VirtualHost 0.0.0.0:443&gt; 的 ServerAlias 值是多少?
  • @Capsule,我认为它也可以,但它似乎不适用于example -> example.com。我已经添加了我的 vhost 文件的内容以进行进一步说明。即使在&lt;VirtualHost *:80&gt; 下,这条规则还会生效吗?我是新手,所以任何额外的解释都将不胜感激!

标签: apache mod-rewrite vhosts


【解决方案1】:

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

这是无效的:

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

实际上你不能在RewriteRule部分使用%{HTTP_HOST}%{REQUEST_URI}

这就是为什么它将您重定向到一个不存在的主机,因此出现connection refused 错误。

您应该在 ssl 虚拟主机中使用此规则:

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

还有这个非 ssl 之一:

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

HTH

【讨论】:

  • 不客气,但实际上,如果你认为你可以在重写规则中使用%{HTTP_HOST}%{REQUEST_URI},那么奇怪的是在你改变它之前它不起作用。调试重定向并查看它想带你去哪里会很有趣。
猜你喜欢
  • 2017-10-26
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 2019-11-11
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多