RewriteCond %{HTTP_HOST} !^(www\.)?(domaintoavoid1|domaintoavoid)\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(www\.)?(domaintoavoid1|domaintoavoid2)\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https%1://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I currently have this www/https redirection, but I have certain domains on which I didn't set up a SSL certificate and I'd like to be able to bypass the redirect entirely. It isn't currently working though, domaintoavoid.com and www.domaintoavoid.com still get redirected to https.
Have it like this in separate rules
# skip these domains from https/www rule
RewriteCond %{HTTP_HOST} ^(?:www\.)?(?:domaintoavoid1|domaintoavoid)\.com$ [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Make sure to clean your browser cache before testing this.
Related
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]
I had the following .htaccess code working with 2 RedirectRules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_METHOD} !=POST
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
But I want to make an optimized solution.
I came up with this based on my previous htaccess, but this is not working, and I would like to know why?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
Another variation (preferred), not working:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%1/$1 [NE,R=301,L]
Browser gives "Corrupted Content Error" message.
You may be able to use this combined rule:
## remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure to use a new browser for testing the change.
I have inherited the following htaccess, and am having issues getting it to always redirect to https.
Currently it allows for both http://www and https://www, but I'm wanting it to always redirect to https://www (subdomains should always redirect to https://subdomain.example.com)
All my attempts so far have caused 500 errors, so I'm obviously missing something.
AddDefaultCharset UTF-8
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} !sub/
RewriteRule (.*) /sub/$1 [L]
You can tweak your rules as:
AddDefaultCharset UTF-8
Options +FollowSymLinks
RewriteEngine On
# handle sub-domains
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# handle main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub/ [NC]
RewriteRule (.*) sub/$1 [L]
Good day,
I have searched for a solution but all the answers don't work as advertised :-).
The code below works for me to:
Redirect all access to HTTPS
Allow subdomains to bypass HTTPS
What I need is the directory Payments/Return-Url to also be allowed to bypass HTTPS and allow HTTP access or pass posted values (P instead of L on the RewriteRule setting breaks the site completely).
RewriteEngine On
/removing www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
/redirecting to https if http
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]
# allowing http on sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://%1.domain.com%{REQUEST_URI} [R=301,L,NE]
I have tried the following edits:
RewriteEngine On
RewriteRule ^(Payments/Return-Url)($|/) - [L]
.......
and also
..........
# allowing http on sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{REQUEST_URI} ^Payments/Return-Url [NC]
RewriteRule ^ http://%1.domain.com%{REQUEST_URI} [R=301,L,NE]
I am a complete noob with this... I tried to understand the documentation, but it takes me a while to absorb and I need this working asap...
I'm currently using the following to force www. however I want to also force https
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How would I edit that correctly to force https AS WELL as www.
Fixed main site with:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Other domains on are now forced to include https I only want my main site to do it...
A different fix for main site:
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^DOMAIN\.com$ [NC]
RewriteRule ^.*$ https://www.DOMAIN.com%{REQUEST_URI} [R,L]
Seems to work correctly. I moved my other domain to a different server
Here is a rule that you can use to force www and https in same rule:
# https and www in one rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
Ordering of RewriteCond is very important here otherwise %1 will not be captured correctly. First 2 conditions are or ORed together and 3rd condition is ANDed.