.htaccess Redirect domain subdomain to sub directory - apache

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]

Related

Redirect a domain request to a subfolder

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]

htaccess subpage on subdomain to subpage on other subdomain

I have trouble finding out the right approach to make a 301 redirect from one subdomains page to another subdomains page.
For instance:
subpage1.mysite.com/contact
needs to be redirected to
subpage2.mysite.com/kontakt
(i have a few hundred of these)
I have a lot of 301 redirects in my .htaccess so far, but they only redirect from the maindomain mysite.com/page.
I cant make a 301 like this, right?
It does not work..
301
Redirect 301 http://subpage1.mysite.com/contact http://subpage2.mysite.com/kontakt
I only can access the primary htaccess file on the main domain (mysite.com)
Read some about RewriteCond but it seems they take all of my subpages and redirect them to the other subdomain. That is not exactly suitable for me, as the url's have changed names and are not identical on the subdomains.
I've figured it out by using this.
Taking the subdomain as RewriteCond
#Consultancy redirects
RewriteCond %{HTTP_HOST} ^consultancy.madebydelta.com [nc]
RewriteRule ^about/$ http://testlab.madebydelta.com/om-os/ [R=301,NC,L]
RewriteRule ^industries/$ http://testlab.madebydelta.com/cases/ [R=301,NC,L]

What in .htaccess redirect subdomain apache

What I must to do, when I have got DNS to subdomain and when someone go to blog.example.com the website url is the same but the content is from the main page not from blog folder.
What I should to do? What must be in /var/www/blog/ in .htaccess if anything?
Please help!
You can use the following rule :
RewriteEngine On
#if host is blog.domain.com
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
#then serve all requests from the Root folder.
RewriteRule ^(.*)$ /$1 [NC,L]
Replace blog.example.com with your subdomain in the http_host string.

htaccess redirect on a domain that already redirects

We have 3 domains in play here. Domain1.com is the main site, domain2.com is the smaller second site, and domain3.com is a vanity URL that redirects from domain1 to a Spanish version of domain2.
What we need to have happen is for domain3.com/subpage to redirect to domain2.com/subpage, however the original redirect of the vanity URL is overriding any attempts we make.
Is this kind of redirect possible?
Here is the original redirect in domain1.com's htaccess file:
RewriteCond %{HTTP:Host} ^www\.domain3\.com$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version [I,R]
These are the attempts at making the needed redirect happen (also in domain1.com's htaccess file):
RewriteCond %{HTTP:Host} ^www\.domain3\.com/subpage$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version/subpage [I,R]
RewriteRule ^domain3\.com/subpage?$ /spanish-version/subpage [NC,R=302,U]
Rules for your domain3 should look like this :
RewriteCond %{HTTP:HOST} ^(www\.)?domain3\.com$ [NC]
RewriteRule ^/?(subpage_name)/?$ http://www.domain2.com/$1 [NC,R,L]
This will redirect
www.domain3.com/subpage
to
www.domain2.com/subpage
(www.)? in HTTP:HOST variable is to make the "www" part optional in url, so in this case
urls with or without "www" would also work.

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]