Redirecting http:// and http://www to https://www without affecting other subdomains on the website

StackOverflow https://stackoverflow.com/questions/23448636

Pergunta

I am having two problems with htaccess on my website

  1. Redirecting

http://example.com => https://www.example.com

http://www.example.com => https://www.example.com

https://example.com => https://www.example.com

I have in my .htaccess:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

It works well on all browser except when i try accessing example.com on Mozilla Firefox where it rewrites to https://www.example.com/https://example.com/

  1. I do not want the htaccess to affect other subdomains on my site. example i do not want http://subdomain.example.com to be redirected.

My website supports wildcard subdomains so I do not rewrite all subdomains.

Foi útil?

Solução

I guess this is what you are looking for:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

I have no idea about that mozilla exception you mention. A caching issue maybe? You 'd need to take a closer look at the log files and maybe you have to enable rewrite logging to see what exactly is going on inside the rewriting engine...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top