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

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]

Related

shared htaccess: redirect multiple domain roots

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]

Apache redirect not working

I have two domains example.com and example.net using the same .htaccess file in public_html directory. I want any valid url like example.net/valid-url to redirect to example.com/valid-url.
I tried the following:
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
rewriterule ^(.*)$ http://www.example.com/$1 [R=301,L]
However the redirect is happening (with above code) to example.com not,
example.com/valid-url.
Is there something I'm doing wrong?
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache before testing this.

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 redirect request from specific domain to another domain

I have an apache server hosting a site. I have 2 domains pointing to this server, www.mysite.se and www.mysite.com.
I'm trying to figure out how to in the htaccess file to redirect traffic coming from the www.mysite.se domain to www.mysite.com/se/
I've tried several ways but cannot get it to work. I always just end up on the root of the site, as in www.mysite.com instead of the /se/ path.
This is what I've tried so far:
RewriteEngine On
RewriteCond %{http_host} ^www\.mysite\.se [NC]
RewriteRule ^(.*)$ http://www.mysite.com/se/ [R=301,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$ [NC]
RewriteRule ^(.*)$ /se/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule ^(/)?$ se [L]
What am I doing wrong?
This seems to have finally done it!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule (.*)$ http://www.mysite.com/se/ [R=301,L]