簡體   English   中英

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

[英]Redirect all to https://www.example.com

好的,所以我知道這個問題的幾乎所有種類都在這個論壇上被問過和回答過,還有很多其他的,但我似乎無法正確回答。 我希望如果我提供足夠的細節,包括我遵循的流程,那么有人能夠發現我的錯誤並糾正我。 開始!

細節

  • Drupal 多站點
  • 阿帕奇網絡服務器
  • 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 -> https://www.example.com重寫。 我已經嘗試了許多在 Internet 上找到的<VirtualHost 0.0.0.0:443><VirtualHost *:80>指令下的示例,但都沒有成功。

此外,每次編輯 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>

好的,現在我知道你的虛擬主機的內容了。

這是無效的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM