Redirect a domain request to a subfolder - apache

I am trying to redirect a domain to a subfolder within the same server. Let me break it down on topics:
I have the domain domain.com
I created the subdomain subdomain.domain.com
I redirected (A record) subdomain.domain.com to the domain.com server's IP address
I am trying to create a htaccess file that redirects every user that comes from subdomain.domain.com to domain.com/aaa
I can't figure out how though. I suppose I need to create a rewriting rule but don't know how.

To Redirect sub.example.com to example.com/folder you can use the following Rule in your /public_html/. htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule (.*) http://example.com/folder/$1 [L,R=301,NE]

Related

.htaccess redirect folder to its subdomain causes redirect loop

I have a subdomain sub.domain.com
And the same contents are available on domain.com/sub
But now I want to redirect domain.com/sub to sub.domain.com
If I do that with
Redirect 301 /sub http://sub.domain.com
it causes a redirect loop.
How can this be solved technically? Users should not be able to access the subdomain content under domain.com/sub but should be redirected.
Better to use mod_rewrite rules here with conditions to restrict the rule only for domain.com requests:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*)?$ http://sub.domain.com/$1 [L,NC,R=302]
Make sure to test this after clearing browser cache.

OpenShift and redirect in .htaccess

I have a Wordpress app deployed on OpenShift and a domain alias associated, i.e. www.example.org. Now I would like to add another alias, i.e. www.example2.org, and gracefully redirect all the request from www.example.org => www.example2.org.
I tried to do this via .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.org$
RewriteRule (.*)$ http://www.example2.org/$1 [R=301,L]
Whenever contact www.example.org it generates an infinite loop of redirects and I can not understand why.
Openshift rewrites the redirect header, but you can prevent this by adding the port number to the URL. See more here.
There's no need for RewriteCond. And example.org is not an alias of example2.org. It's a brand new A record. You can eventually redirect www.example.org via your domain registrar's dashboard or create index.php with header('Location: http://www.example2.org');.
RewriteEngine on
RewriteRule ^(.*)$ http://www.example2.org/$1 [R,QSA,L]

.htaccess Redirect domain subdomain to sub directory

We'd like to redirect a subdomain to a subdirectory:
customerservice.example.com to example.com/customerservice.
How is this done in .htaccess file?
I assumed:
Redirect 301 customerservice.example.com example.com/customerservice
Am i correct in my assumption?
Redirect does not take a full domain as first argument, you have to use /:
Redirect 301 / http://example.com/customerservice
However if this domain is on the same root of another domain and you need to match the domain name you will have to use something like this:
RewriteCond %{HTTP_HOST} ^customerservice\.example\.com$
RewriteRule ^(.*)$ http://example.com/customerservice/$1 [R=301,L]

301 redirect addon domain improperly

I have a website with url http://abc.com/myfolder/, now i add a addon domain "xyz.com" in cpanel and point it to myfolder. The addon domain is working successfully. xyz.com is showing what http://abc.com/myfolder/ showing. However http://abc.com/myfolder/ is already indexed in google, thus i want to do a 301 redirect from http://abc.com/myfolder/ to xyz.com. I searched online and did something like below.
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^(.*)$ http://www.xyz.com/$1 [R=301,L]
but the redirect failed. showing "The page isn't redirecting properly". What can i do
you have first to check that the host is abc.com before applying the rule, otherwise, you end up applying the rule for both abc.com and xyz.com which result an infinite redirection loop.
RewriteEngine On
RewriteCond %{HTTP_HOST} abc.com$
RewriteRule ^myfolder/?(.*)$ http://www.xyz.com/$1 [R=301,L,NC]

Redirecting base url only using htaccess

I'd like to redirect only the base url to an external site.
For instance, I want example.com redirected to anotherdomain.com but I don't want example.com/path to be redirected.
So far, example.com/path redirects to anotherdomain.com/path. :(
EDIT :
First, thank you for the help! example.com now redirects to another.com without affecting the children paths of example.com.
However, ideally, m.example.com won't redirect to another.com. So it's really just example.com redirecting to another.com.
Add this to your .htaccess in your DocumentRoot. I am assuming that you are hosting only one domain on the server.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^$ http://anotherdomain.com [R,L]