How to exclude subdomains from redirecting? - regex

I have this code to force any URL to be "https://www.":
#rewrite to WWW:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have sub-domains and I do not want any of them to be redirected. What can I do?

You can do it all in single rule:
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(?:static\.|subdomain1\.|subdomain2\.|\d+) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache before testing this rule.

Related

Redirect all http/www into https://www

I have this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
What this does is:
With http://example.com it redirects to https://www.example.com (this is correct)
With https://example.com it redirects to https://www.example.com (this is correct)
But with http://www.example.com it doesn't redirect to https://www.example.com.
Please take note that It needs to be not more than 1 chain. It should redirect to https://www.example.com.
You can avoid hardcoding your host name and do both conditions in a single redirect rule like this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Okay I just fixed it.
this is the updated code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
I can guarantee that this will all do 301 redirect without more than 1 chain.
I hope this helps some developers out there!.

Htaccess : https for all except specific domains

I have the following rules to redirect all request to https://www.mainsite.com
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now I want to redirect http://another-domain.com to a specific folder (blog) but without https so I added this. but
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?another-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/$1
RewriteCond %{HTTP_HOST} ^(www.)?another-domain$ [NC]
RewriteRule ^(/)?$ blog/index.php [L]
Even with that http://another-domain.com is still redirected to https://www.mainsite.com
Have it this way:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?!(www\.)?another-domain\.com$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?another-domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!blog/).+)$ blog/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?another-domain\.com$ [NC]
RewriteRule ^/?$ blog/index.php [L]

apache .htaccess rewrite should not include www

We have a domain test.com, we want to redirect http://www.test.com to http://test.com and https://www.test.com to https://test.com, We can achieve the same by below mentioned rule, but we dont want http:// and beside what do we have to do for https://
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
You can have a single rule to handle both http and https traffic:
RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=302,L,NE]
If you want everything to go to https, then you need to change your rule to:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
Otherwise, you'll need separate rules for http and https:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

htaccess redirect conflict/not working

I have a QR code that I need to redirect to a new page. For some reason the redirect is being overridden (ends up at cmdgroup.com/community instead of the appropritate page)
RewriteCond %{HTTP_HOST} ^(.*)?companies/Inova-Solutions-Inc/Network-Clocks-by-Inova-Solutions/53e4f5e048e43c1d5cef656d [NC]
RewriteRule ^(.*)$ http://www.cmdgroup.com/companies/1142841/inova-solutions-inc/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)?community.cmdgroup.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*)?community.reedconstructiondata.com [NC]
RewriteRule ^(.*)$ http://www.cmdgroup.com/community/ [R=301,L]
It is because your first RewriteCond %{HTTP_HOST} is wrong since you only match domain name using %{HTTP_HOST} variable. Change that rule to this:
RewriteRule companies/Inova-Solutions-Inc/Network-Clocks-by-Inova-Solutions/53e4f5e048e43c1d5cef656d$ http://www.cmdgroup.com/companies/1142841/inova-solutions-inc/ [R=301,L]
RewriteCond %{HTTP_HOST} community\.(cmdgroup|reedconstructiondata)\.com$ [NC]
RewriteRule ^ http://www.cmdgroup.com/community/ [R=301,L]

disable all subdomains except pre-defined in htaccess

The below code disables subdomains, but I can't figure out how to add an exception for a specific subdomain.
I searched for questions on this subject and could not find anything.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Subdomain Exceptions...
RewriteCond %{HTTP_HOST} !^widgets\.hyperbot\.tv$
# Disable Subdomains
RewriteCond %{HTTP_HOST} !^www\.hyperbot\.tv$
RewriteRule (.*) http://www.hyperbot.tv/ [R=301,L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !(www\.)?hyperbot\.tv [NC]
RewriteCond %{HTTP_REFERER} !google\. [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule (.*) - [F,NC,L]
RewriteCond %{QUERY_STRING} proc/self/environ [OR]
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})
</IfModule>
To disable all subdomains except one you can use:
RewriteCond %{HTTP_HOST} (^|\.)hyperbot\.tv$ [NC]
RewriteCond %{HTTP_HOST} !^(www|widgets)\.hyperbot\.tv$ [NC]
RewriteRule ^ http://www.hyperbot.tv%{REQUEST_URI} [NE,R=301,L]
try this code
RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.tld$ [NC]
RewriteRule ^ - [F,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]