Advice on Configuring .HTaccess file to Redirect HTTP Subdomain to HTTPS Equivalent

我的梦境 提交于 2020-01-06 05:59:50

问题


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:

  1. hxxp://subdomain1.domain.com --> hxxps://subdomain1.domain.com
  2. hxxp://subdomain2.domain.com --> hxxps://subdomain2.domain.com

...and so on, but also...

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!