I have set up my main site to use https. And I force the user over to https with .htaccess. I use this code
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
However I have a sub domain. That is called xxx . It's content is located in ./xxx . How can I force xxx.domain.com to only use http?
You can use this
RewriteEngine on
#redirect www urls to non-www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# do nothing if sub.domain.com is requested
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^ - [L]
#redirect the main domain to https
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
Clear your browser cache before testing this.
Related
I am trying to redirect:
http://website.com & http://website.com/index.php to https://website.com
All the solutions I found will redirect http://website.com/test.php (that has login) to a 401 page. I just want / and /index.php to redirect to HTTPS
The following code does not work for my needs:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
and
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
You may use this rule as your topmost rule:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^(?:index\.php)?$ https://%{HTTP_HOST} [L,NE,R=301]
Make sure this is top rule and you test it from a new browser.
I have the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
to redirect from non-www to www
On this question, I found the following code, to make such redirection, and also http to https redirect, without casuing a double redirection:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
But I don't want the example.com part, I need a flexible code.
So I tried this code:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,NE,L]
something in syntax is bad, and it doens't work as expected.
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Just use this in your .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
Using %{HTTP_HOST} will grab the host instead of implicitly calling example.com. Make sure you clear your cache before testing this.
EDIT BASED ON COMMENTS
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%1.%2/$1 [R=301,NE,L]
Those are the lines of concern currently in my .htaccess file,
note that i have two domains pointing to the same site:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R]
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
Now I need to convert my site to SSL, I was instructed to use the following
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think that this code misses my other domain and the www rewrite part,
how to enable SSL while accounting for my other domain and also for the WWW part?
You can use:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]
I’m trying to do that:
Force the https for my main domain.
http or https://www.domain.com -> https://domain.com
http or https://domain.com -> https://domain.com
But not for subdomains
http or https://www.subdomain.domain.com -> http://subdomain.domain.com
http or https://subdomain.domain.com -> http://subdomain.domain.com
And always removing the www.
Now i have that:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This redirects www to non-www and http to https but not for subdomains. The subdomain remains with www and the https.
Thanks
You can use these 2 rules:
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
In order to get the combination you want. You will need to use your domains.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.example\.com [NC]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://sub.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^sub\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I also used the following and it worked great. I was having a hard time pointing my primary domains www.domain.com and http://domain.com to https://domain.com
Thanks very much; as I've been trying to get this done for hours now!!!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
I want to remove the www from https url by .htaccess.
for example
i want to change
https://www.example.com/ to https://example.com/
I have tried many rules
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.come/$1 [L]
and
RewriteCond %{SERVER_PORT} ^443
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://example.com$1 [L,R,QSA]
and
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But nothing work. Please help me
Try:
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
Please make sure if you are using X-Forwarding then try this one:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://example.com/$1 [R,L]