简体   繁体   中英

How to redirect all traffic to https except subdomains

I have recently redirect all traffic to https but I want to keep my subdomains on http only. Please help me to sort out this.

this is my rewriterules

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]

this is my virtual host code

<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>

Please help me to sort out this. Thanks in advance.

You have two choices.

1. Either you add a condition in your file to only match main domain

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

Note that the rule to match www main domain can be removed since your main domain is example.com (no trace of www in your vhosts).

Warning: useless in some cases, you should not include the same file in all vhosts (see next point).

2. Or you can directly put a rule in example.com (http) vhost block. Indeed, others two ( example.com for https and stagging subdomain) do not require rules to do what you want.

So, your config should look like this

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM