问题
I'm sure there is an easy answer to this question, but I've scoured through through available threads and its either not there or I'm too ignorant to recognize it starting in my face. This seems to be the answer, but I just can't configure correctly.
Question: How do I set up .htaccess so that all subdomains are forwarded to their https equivalents, while also allowing the main domain itself get ported to its https equivalent? In other words:
- hxxp://subdomain1.domain.com --> hxxps://subdomain1.domain.com
- hxxp://subdomain2.domain.com --> hxxps://subdomain2.domain.com
...and so on, but also...
- hxxp://domain.com --> hxxps://domain.com
Current Configuration: My .htaccess is set up to provide an unconditional forward as follows:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
To achieve what I want, I would think that there must be some way to define a wildcard and pass the result of that wildcard as the subdomain - whether, subdomain1, subdomain2 or a value of nothing - to the actual redirect.
Any help would be much appreciated.
回答1:
I haven't tested this, but it should allow you to capture the subdomain as well as the request.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.*)\.yourdomain\.com$
RewriteRule ^(.*)$ https://%1.domain.com/$1 [R,L]
This rule however, will not work for requests that do not contain a subdirectory, so you actually need two rules.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
来源:https://stackoverflow.com/questions/5365612/advice-on-configuring-htaccess-file-to-redirect-http-subdomain-to-https-equival