全部重定向到 https://www.example.com
Redirect all to https://www.example.com
好的,所以我知道这个问题的几乎所有种类都已在这个论坛和许多其他论坛上提出和回答,但我似乎无法正确回答。我希望如果我提供足够的细节,包括我正在遵循的过程,那么有人将能够发现我的错误并纠正我。开始了!
细节
- Drupal 多站点
- Apache 网络服务器
- Aegir 提供的站点
我想完成的与我已经完成的
我想将所有 http 和 https 流量定向到 https://www.example.com。以下标有 +
的示例已经完成;标有 -
的那个让我望而却步:
+
http://example.com -> https://www.example.com
+
http://www.example.com -> https://www.example.com
+
https://www.example.com -> https://www.example.com #redundant, but at least we know it works
-
https://example.com -> https://www.example.com
我在做什么
每个站点都有一个由 Aegir 生成的 vhost 文件,每个 vhost 文件都包含两个 VirtualHost 指令:
<VirtualHost 0.0.0.0:443>
<VirtualHost *:80>
我修改了example.com
的虚拟主机文件,在<VirtualHost *:80>
指令下包含了以下代码,成功实现了上面的三个工作示例:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
问题
我似乎无法弄清楚如何执行 https://example.com -> https://www.example.com 重写。我尝试了很多在 <VirtualHost 0.0.0.0:443>
和 <VirtualHost *:80>
指令下在 Internet 上找到的示例,但没有成功。
此外,每次编辑 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 [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 [R=301,L]
而这一个在非 ssl 中:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com [R=301,L]
HTH
好的,所以我知道这个问题的几乎所有种类都已在这个论坛和许多其他论坛上提出和回答,但我似乎无法正确回答。我希望如果我提供足够的细节,包括我正在遵循的过程,那么有人将能够发现我的错误并纠正我。开始了!
细节
- Drupal 多站点
- Apache 网络服务器
- Aegir 提供的站点
我想完成的与我已经完成的
我想将所有 http 和 https 流量定向到 https://www.example.com。以下标有 +
的示例已经完成;标有 -
的那个让我望而却步:
+
http://example.com -> https://www.example.com+
http://www.example.com -> https://www.example.com+
https://www.example.com -> https://www.example.com#redundant, but at least we know it works
-
https://example.com -> https://www.example.com
我在做什么
每个站点都有一个由 Aegir 生成的 vhost 文件,每个 vhost 文件都包含两个 VirtualHost 指令:
<VirtualHost 0.0.0.0:443>
<VirtualHost *:80>
我修改了example.com
的虚拟主机文件,在<VirtualHost *:80>
指令下包含了以下代码,成功实现了上面的三个工作示例:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
问题
我似乎无法弄清楚如何执行 https://example.com -> https://www.example.com 重写。我尝试了很多在 <VirtualHost 0.0.0.0:443>
和 <VirtualHost *:80>
指令下在 Internet 上找到的示例,但没有成功。
此外,每次编辑 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 [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 [R=301,L]
而这一个在非 ssl 中:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com [R=301,L]
HTH