Exclude directory from redirecting to https - .htaccess

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

Related

Htaccess Redirect / and index.php to HTTPS

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.

Redirect Any Domain from non-www to www and to HTTPS without double redirect

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]

.htaccess force http for sub domain

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.

htaccess redirect avoid specific domains

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.

mod rewrite Force HTTPS + WWW

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.

Resources