Apache redirect rule optimize - apache

I'm trying to optimize my redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} !^boycottplus\.org$
RewriteRule ^ https://boycottplus.org%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Part 1 is to redirect users that visit the .com site to the .org site
Part 2 is to redirect users that visit unsecure (http) to https
Is there a way to both redirect .com user to the secure .org site, and redirect http users on the .org site to the https site? Both while keeping the {request_uri} intact?

Use both conditions in one rule while applying OR logic instead of default AND:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^boycottplus\.org$ [OR]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://boycottplus.org%{REQUEST_URI} [L,R=301]

Related

.htaccess - multiple page by page redirects + www & https rules

Been struggling and playing with this and can't get it working in Apache.
I'm not well versed in the syntax and was rather trying to build something based on existing questions on here.
Trying to force everything to HTTPS and www, and also add over 200 individual page redirects.
RewriteEngine on
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /PAGE1 /PAGE2
Thanks a lot in advance!
in thw www part remove the https condition and redirect with https and www anyway
in the https part remove the www condition
after those 2 ... continue with all your paged redirecting
RewriteEngine on
# Redirect to domain with HTTPS.
RewriteCond %{HTTPS} off
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /PAGE1 /PAGE2

https for single domain .htaccess

I have a multiple addon domains on my hosting account. I would like to redirect non-https to https for the main domain i.e
andamanexoticholidays.com
my .htaccess approach:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but this makes all adon domain to force use ssl by default.
my addon domain are:
radhakrishnaresorthavelock.com
and my sud domains are:
agents.andamanexoticholidays.com
and I want to force use
https://www.andamanexoticholidays.com
a https and www.
any help would be appreciated.
Use a RewriteCondition so that only the main domain can get redirected to https , try :
RewriteCond %{HTTP_HOST} ^(www\.)?andamanexoticholidays.com$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.andamanexoticholidays.com %{REQUEST_URI} [L,R=301]

.htaccess 301 redirect issue with https Urls

we want to redirect our .com domain name to .net domain, HTTP work for us, but when user use HTTPS path, they redirect to 404 page of .com domain. Please check our code and help me, how to redirect .com domain HTTP and HTTPS both user to some other .net domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.net\/$1" [R=301,L]
Replace your rule by this rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://domain.net%{REQUEST_URI} [L,R=301,NE]

Apache SSL Rewrite with Wildcard Subdomains

I'm attempting to setup an SSL redirect in Apache using RewriteEngine that will do the following:
Redirect traffic to either http://mydomain.com or http://www.mydomain.com to use HTTPS
Redirect traffic to any other subdomain https://*.mydomain.com to use HTTP instead of HTTPS
My reasoning for this is that I'm developing a project that's using a free SSL certificate until launch. This certificate covers the base domain, but none of the wildcard subdomains, and it's a pain to need to bypass the warning every time I visit one of the subdomains.
Edit:
I believe I'm close here, but I still can't get the HTTPS to HTTP redirect to work properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Figured it out. By moving these rules from the sites-available/default file to an .htaccess inside of the website root, I was able to get this working properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess redirect HTTPS to secure.domain.com, non https to www.domain.com

Not sure how to best write the following rules:
All secure (HTTPS) traffic should redirect to https://secure.domain.com
Non-secure requests to secure.domain.com should redirect to https://secure.domain.com
All other non-secure requests should go to http://www.domain.com
Lastly, all non-www requests should redirect to www, except the "secure" subdomain.
Try adding the following to your htaccess file in the root folder of your domain.
RewriteEngine on
RewriteBase /
#Non-secure requests to secure.domain.com should redirect to https://secure.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\.domain\.com$ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#All secure (HTTPS) traffic should redirect to https://secure.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^secure\.domain\.com$ [NC]
RewriteRule .* https://secure.domain.com%{REQUEST_URI} [L,R=301]
#All other non-secure requests should go to http://www.domain.com
#Lastly, all non-www requests should redirect to www, except the "secure" subdomain.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .* http://www.domain.com%{REQUEST_URI} [L,R=301]