Force HTTPS and WWW for domain and only HTTPS for subdomains HTACESS

て烟熏妆下的殇ゞ 提交于 2019-12-09 00:58:39

问题


I've been trying to configure this for my website, but not being able to.

I use to have a cond on my .htaccess to force www for the main domain and nothing for subdomains, but since I got a SSL, I'm having some problems.

It's a wildcard SSL.

What I need is force HTTPS:// WWW on the main domain, and HTTPS:// on subdomains.

I.E: http://www.domain.com -> https://subdomain.domain.com

Is there any rule for that? Thanks!

EDITED

Now I'm using like Jon posted

RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [NC]
RewriteRule ^.*$ https://www.domain.com.br%{REQUEST_URI} [R,L]
# ---------- #
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com\.br$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]

The thing is, when on main domain if I type HTTP:// with or without WWW, ir forces HTTPS:// and WWW, that's ok...

But on subdomain, when I type HTTP it doesn't force HTTPS, it redirects to the main domain only... that does not happen if I put a .htaccess inside the dir of the subdomain. With a .htaccess inside my subdomain dir, if I type HTTP, it forces HTTPS normally...

Any suggestions?


回答1:


Try:

RewriteEngine On 

# for subdomains
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]

# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]


来源:https://stackoverflow.com/questions/20894947/force-https-and-www-for-domain-and-only-https-for-subdomains-htacess

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