Redirect subdomain to main domain in Joomla - apache

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/

Related

Want to redirect TLD domain to a subdomain on https with htaccess. Tried htaccess RewriteRule Redirect found but not working

I want to redirect via .htaccess directives all these:
http://domain.com AND www.domain.com AND http://subdomain.domain.com
TO :
https://subdomain.domain.com
Please note that "subdomain" is an unique subdomain, I do not want to redirect any subdomain but only one.
I'm not familiar with .htaccess directives so I tried all RewriteRule or Redirect snippet found here or elsewere, but it's not working. I must miss something.
EDIT: I added a .htaccess into sur directory of subdomain with this and it works now:
It's OK, I added this in htaccess into the subdirectory of subdomain :
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Try with:
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]

Redirect all subdomain to main domain except one selected

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]

only main Subdomain redirect to main domain through .htaccess

I want to redirect only main subdomain like
http://dl.domain.com to domain.com
but not redirect files or script links to main domain.
http://dl.domain.com/123.zip
http://dl.domain.com/zippy.zip etc
How I can do this with .htaccess rules?
I use these .htaccess rules but problem not solve
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteRule !^xyz\.php($|/) http://www.domain.com%{REQUEST_URI} [NE,R=301,L]
You can use:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteRule ^/?$ http://www.domain.com [R=301,L]

htaccess redirect with sub-domain

I have a domain example.com and in .htaccess, I am forwarding non-www to www address using below code.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now I created a subdomain test.example.com and because of above rule, its going to http://www.test.example.com, so I thought to write it's own htaccess file to redirect www to non-www. So in the subdomain root, I have .htaccess file with below rule.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/$1 [R=301,L]
It's not working and going in a redirect loop, how do I fix it?
Take out the ! and the escapes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ http://test.example.com/$1 [R=301,L]
You don't need 2 rules. Just keep this rule in main .htaccess for your main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
Now RewriteCond %{HTTP_HOST} ^example\.com$ condition will only impact main domain leaving your sub domain untouched.
I found a very easy solution for this problem.
My main site document root was /home/example/public_html and subdomain document root was /home/example/public_html/test.
I just changed the document root of subdomain to /home/example/test and it worked.

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.