shared htaccess: redirect multiple domain roots - apache

My situtation is that multiple domains share a htaccess file and I need to redirect the root / to each language folder.
So I want ONLY the root / of each domain to be redirected like:
www.my-en.com/ to redirect to /en/
and
www.my-de.de/ to redirect to /de/
Unfortunately I need to accomplish this with htaccess as I CANNOT change any other settings.
So far all solutions I found look like this
redirct 301 /old-path http://www.new-domain.com
which will always be applied to all domains using the htaccess.

You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?my-en\.com$ [NC]
RewriteRule ^$ http://www.my-en.com/en/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?my-de\.de$ [NC]
RewriteRule ^$ http://www.my-de.de/de/ [R=301,L]
# Redirect others to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

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]

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]

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/

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 force www not for cdn

I have a problem with .htaccess forcing www.
I have the main site http://www.portalesardegna.com and I want to redirect http://portalesardegna.com to the www version, but I have even a CDN, http://cdn.portalesardegna.com, if I look the site with pingdom http://tools.pingdom.com/fpt/#!/eHy16D/www.portalesardegna.com all the files in the cdn are redirected to the www version.
Some information that can be useful:
The CDN is on an external service, and the dns point to another server, not mine.
I have some images from another subdomain in my site, private3.portalesardegna.com, and all of these images are not redirected.
Here is the code i use in the htaccess
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^cdn\..+$ [NC]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Try this rule as as your very first rule in your root .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(cdn|www)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]