Redirect all subdomain to main domain except one selected - apache

If I want to redirect all subdomains to a main domain, except one subdomain that I need redirect to a subfolder. Is there any way?
Redirect selected subdomain to subfolder
Redirect all subdomains to main domain
This is my Apache configuration:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/mySubdomain [L,R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule (.*) http://domain.com/$1 [L,R=301,QSA]

The first rule is correct to serve a particular subdomain from a directory. For the second rule just add 1 AND condition that if the current request is not for domain.com AND not for mySubdomain.domain.com then redirect request to domain.com as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/mySubdomain/$1 [R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

Related

htaccess rewrite rules - domain/subdomain https/www

I have my main domain and a subdomain. Subdomain is located in a subdirectory on the root directory of main domain and this cannot be changed. Main domain has an SSL while subdomain doesn't.
Now I am trying to force https and www for the main domain and force no https and no www for the subdomain. So at the end the main domain disregarding how it will try to be accessed should redirect at https://www.example.com and same time subdomain should be http://sub.example.com
All other possible combinations of accessing them should redirect to the above 2. So far, I am successfull to do this for the most possibilities but I am failing to do so for the case when I access the subdomain with https://sub.example.com. It won't redirect to the http version, rather it will try to load the page through https, browser throws the security warning about the SSL and if I proceed, it load the contents of the main domain site through the subdomain and the insecure warning on the browser.
Following is the htaccess rules I am trying, on the htaccess on the root directory, that affects both main site and subdomain.
How to improve them and achieve the desired outcome?
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\.example\.com [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !sub\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www\.sub\. [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
To redirect only the main domain to https, you can use the following rule :
RewriteEngine on
#https to http (subdomain)
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule ^ http://sub.example.com%{REQUEST_URI} [NE,L,R]
#main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

Redirect subdomain to main domain in Joomla

I need to redirect all subdomains of my website to the main domain starting with www:
I have tried to add this code to my .htaccess:
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
but I have a redirect loop error.
What do you suggest?
Thanks
Your rule will indeed cause redirect loop. Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
If you want demo.domain.com to be redirected to www.domain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301,NE]
I'm using this code in my .htaccess file at the root directory of the domain or subdomain
Redirect all requests from subdomain:
Redirect / http://www.example.com/
Redirect all requests from folder sub on the domain to the subdomain:
Redirect /sub http://sub.example.com/

how to redirect www subdomain to non-www when domain is redirected to www

My primary domain is currently permanently redirected to www.mydomain.com (non-www to www redirection), with .htaccess as follows:
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^/?$ "http\:\/\/www\.mydomain\.com\/" [R=301,L]
RewriteCond %{HTTP_REFERER} !^http://mydomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://mydomain.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com$ [NC]
I would like to know how all subdomains that I'll be creating, ex. blog.mydomain.com, will be redirected to non-www, ex. blog.mydomain.com, and not www.blog.mydomain.com. Every time I create a subdomain and enter the non-www URL to the browser, it prompts a redirect loop.
Hope you can help! Thanks! :)
Keep this one rule for all the sub-domains:
# rule for forcing www on main domain
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# rule for removing www on sub domains
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.mydomain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
This one supports http + https in one line:
# Redirect www subdomain to non-www
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.yourdomain\.com)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [R=301,L]

Apache: redirect if host doesn't start with www or cdn

My goal is to redirect to domain with www. if both these conditions are met:
current host is not www.example.com
current host is not cdn.example.com
I'm using a .htaccess file with this code, but the second condition is not honoured:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^cdn\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Thank you
Using this did the trick:
RewriteCond %{HTTP_HOST} !^(cdn|www)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
I already tried it before but was tricked --by a cached redirect-- to think it was not working.

htaccess: exluce some domain in RewriteCond

this is my .htaccess code
so if the user type just domain.com will be redirected to www.domain.com
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
my problem now is that i have a new domain pointing to the same domain path
so even the new domain is redirected "transparently" to domain.com...
how can i exclude some domain name from that rule?
thanks!
You could try making the rewrite generic, so all requests that are not starting with www are redirected, but on the correct/requested domain.
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
Or, you can check instead for if the domain starts with domain.com:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
Hopefully this helps.