【发布时间】:2015-12-06 04:08:54
【问题描述】:
好的,所以我知道在这个论坛和许多其他论坛上,几乎所有的这个问题都被问到并得到了回答,但我似乎无法正确回答。我希望如果我提供足够的细节,包括我正在遵循的过程,那么有人将能够发现我的错误并纠正我。来了!
细节
- Drupal 多站点
- Apache 网络服务器
- Aegir 提供的网站
我想要完成的 vs 我已经完成的
我想将所有 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的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>
【问题讨论】:
-
https://example.com->https://www.example.com应该由你的第一条规则来处理。<VirtualHost 0.0.0.0:443>的 ServerAlias 值是多少? -
@Capsule,我认为它也可以,但它似乎不适用于example -> example.com。我已经添加了我的 vhost 文件的内容以进行进一步说明。即使在
<VirtualHost *:80>下,这条规则还会生效吗?我是新手,所以任何额外的解释都将不胜感激!
标签: apache mod-rewrite vhosts