如何将所有流量重定向到除子域以外的https

时间:2017-05-23 12:09:15

标签: .htaccess http redirect https

我最近将所有流量重定向到https,但我想将子域保留在http上。请帮我解决这个问题。

这是我的重写

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

这是我的虚拟主机代码

<VirtualHost *:80>
ServerAdmin  test@example.com
ServerName   staging.example.com
ServerAlias  staging.example.com
SetEnv       ENVIRONMENT staging
DocumentRoot /var/www/staging/
ErrorLog     /var/log/apache2/example_staging_error.log
CustomLog    /var/log/apache2/example_staging_access.log combined

<Directory /var/www/staging>
AllowOverride All
Include /etc/apache2/sites-available/example_dir_rules.conf
</Directory>


</VirtualHost>

<VirtualHost *:80>
ServerAdmin  test@example.com
ServerName   example.com
ServerAlias  example.com
SetEnv       ENVIRONMENT production
DocumentRoot /var/www/production/
ErrorLog     /var/log/apache2/example_production_error.log
CustomLog    /var/log/apache2/example_production_access.log combined

<Directory /var/www/production>
Include /etc/apache2/sites-available/example_dir_rules.conf
</Directory>



</VirtualHost>
LoadModule ssl_module /usr/lib/apache2/modules/mod_ssl.so
#Listen 443
<VirtualHost *:443>
SSLEngine on

SSLHonorCipherOrder On

-------------ssl code----------

ServerName example.com

-------------ssl code----------

ServerAdmin  test@example.com
ServerName   example.com
ServerAlias  example.com
SetEnv       ENVIRONMENT production
DocumentRoot /var/www/production/
ErrorLog     /var/log/apache2/example_ssl_error.log
CustomLog    /var/log/apache2/example_ssl_access.log combined


<Directory /var/www/production>
Include /etc/apache2/sites-available/example_dir_rules.conf
</Directory>


</VirtualHost>

请帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:2)

你有两个选择。

1。您在文件中添加条件仅匹配主域

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

请注意,可以删除匹配www主域的规则,因为您的主域名为example.com(您的虚拟主机中没有www的跟踪)。

警告:在某些情况下无用,您不应在所有虚拟主机中包含相同的文件(请参阅下一点)。

2. 或者您可以直接example.com(http)vhost块中添加规则。实际上,其他两个(https和example.com子域stagging)不需要规则来执行您想要的操作。

所以,你的配置应该是这样的

<VirtualHost *:80>
ServerAdmin  test@example.com
ServerName   staging.example.com
ServerAlias  staging.example.com
SetEnv       ENVIRONMENT staging
DocumentRoot /var/www/staging/
ErrorLog     /var/log/apache2/example_staging_error.log
CustomLog    /var/log/apache2/example_staging_access.log combined

<Directory /var/www/staging>
</Directory>


</VirtualHost>

<VirtualHost *:80>
ServerAdmin  test@example.com
ServerName   example.com
ServerAlias  example.com
SetEnv       ENVIRONMENT production
DocumentRoot /var/www/production/
ErrorLog     /var/log/apache2/example_production_error.log
CustomLog    /var/log/apache2/example_production_access.log combined

<Directory /var/www/production>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</Directory>



</VirtualHost>
LoadModule ssl_module /usr/lib/apache2/modules/mod_ssl.so
#Listen 443
<VirtualHost *:443>
SSLEngine on

SSLHonorCipherOrder On

-------------ssl code----------

ServerName example.com

-------------ssl code----------

ServerAdmin  test@example.com
ServerName   example.com
ServerAlias  example.com
SetEnv       ENVIRONMENT production
DocumentRoot /var/www/production/
ErrorLog     /var/log/apache2/example_ssl_error.log
CustomLog    /var/log/apache2/example_ssl_access.log combined


<Directory /var/www/production>
</Directory>


</VirtualHost>