Redirect from one domain to another, without the user realizing it - apache

I have a bunch of domains on on of my servers. I'd like to be able to redirect some domains to a different domain without the user knowing, so not a 301 redirect.
An example, redirect domain.com to masterdomain.com/sites/domain.com. So when visiting domain.com, the user would be displayed with the data on masterdomain.com/sites/domain.com.
Also masterdomain.com and domain.com are on the same server.
How could I do this using Apache? mod_rewritem mod_alias?

If both host names use the same document root, you can do this:
RewriteCond %{HTTP_HOST} =example.com
RewriteRule !^sites/example\.com(/|$) sites/example.com%{REQUEST_URI} [L]
This rule will prepend /sites/example.com if the requested URI path does not already starts with that.

GoDaddy calls this a domain Alais.
Here is a link where it is explained
http://webwizardworks.com/web-design/domain-alias.html

Related

301 redirect from another domain

WE have a whole bunch of subdomains from another domain that are pointing to our server. So I am trying to do a 301 redirect from any subdomain at that domain to point to ours
eg.
sub1.domain.com 301 redirect to ourdomain.com
sub2.domain.com 301 redirect to ourdomain.com
There could be any number of subdomains pointing to it.
What should the 301 redirect look like for this?
I have this:
RewriteCond %{HTTP_HOST} *\.domain\.net\.au$ [NC]
RewriteRule ^ https://ourdomain.com%{REQUEST_URI} [R=301,L,NE]
but this returns a internal error, if I replace the * with an actual domain it sort of works.
PArt two of this question is about https, our site is always https, when you click on a link in google that goes to one of these domains, its tries to take them to https://sub1.domain.com
Even when I put in the redirect , it still tries to go to https://sub1.domain.com, which causes the browser to give a "this is not safe" error, is there any way via the 301, to make it so it goes directly to our domain without it giving the https error on the other domain first?
Here is part one:
RewriteCond %{HTTP_HOST} [^.]+\.domain\.net\.au$
RewriteRule ^ https://ourdomain.com%{REQUEST_URI} [R=301,L]
As for part two, no, that can't be done. HTTPS certificate negotiation has to happen before the connection is established to issue the redirect. All you can do is get a wildcard SSL certificate or provide a valid certificate for the subdomains in some other way. It can't be done without a valid certificate unless you accept the "not safe" errors, which most visitors won't. But once the redirects are in place for a while, the listings will be dropped by Google anyway.

Using Apache, how can I redirect a subfolder of a site to a subdomain but redirect everything but that subfolder to the primary domain?

I have the same website running on Apache at two different subdomains using vhosts.
www.vhost1.com
www.vhost2.com
Whenever someone accesses a particular subfolder on www.vhost1.com, I want the user to be redirected to that same subfolder on www.vhost2.com.
Example:
User accesses www.vhost1.com/my-special-folder
User should be redirected to www.vhost2.com/my-special-folder
And whenever someone is on www.vhost2.com and accesses anything except that subfolder, I want the user to be redirected to www.vhost1.com/any-other-url
Example:
User accesses www.vhost2.com/any-other-url
User should be redirected to www.vhost1.com/any-other-url
Also, if the user accesses www.vhost2.com (with no path), the user should be redirected to www.vhost1.com
Example:
User accesses www.vhost2.com
User should be redirected to www.vhost1.com
I've tried several different Apache redirect rules and have gotten several of the scenarios to work, but I can't get them all to work at the same time.
Because of the server configuration, I must perform the redirects using a .htaccess file.
What should the Apache rules be?
Place this rule in root .htaccess of both domains:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?vhost1\.com$ [NC]
RewriteRule ^(my-special-folder)(/.*)?$ http://www.vhost2.com/$1$2 [L,NC,NE.R=302]
RewriteCond %{HTTP_HOST} ^(www\.)?vhost2\.com$ [NC]
RewriteRule ^((?!my-special-folder/).*)$ http://www.vhost1.com/$1 [L,NC,NE,R=302]

Redirect help from old domain to new domain, using cname www, but use htaccess to exclude specific directories?

We moved our website from our server, domainname.com to another server (a SaaS). We pointed the www via CNAME record in WHM (in order to preserve the MX and other services on the "old" server)
ALL www.domain.com traffic is now sent to the new SaaS. Works great.
For non-www links, we are using:
RewriteCond %{HTTP_HOST} !^(.*)\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]
and that works great too.
WE ALSO own domain-name.com, and that also lives on the "old" server.
domain-name.com is a misspelling of our company name, and we want all traffic from www and non-www.domain-name to 301 to domainname.com
That is easy too! In the domain-name .htaccess:
RewriteRule .* http://www.domainname.com/ [R=301,L]
Now the fun (and where I could use some help) --
We have many existing links, such as:
www.domainname.com/images/foldername1/image_300x300.png
www.domainname.com/images/foldername2/image_300x300.png
www.domainname.com/html_pages/page1.html
www.domainname.com/html_pages/page2.html
etc... And I'd really like to preserve these without getting sent to the new SaaS and thereby 404ing.
OR -- as an option, the entire contents of www.domainname.com has been cloned to www.domain-name.com -- so we could 301
http://www.domainname.com/images/foldername1/image_300x300.png to
http://www.domain-name.com/images/foldername1/image_300x300.png
and that would be perfect too... Actually, that would be preferred.
I have tried a dozen suggestions from other doing global redirects... but because the primary www.domainname.com is getting its redirect from CNAME, and we MUST trap for non-www in domainname.com's htaccess, HOW can we constrain specific directories (www or not) to 301 to a new server AND also force www.domain-name.com (and non-www) to 301 to www.domainname.com (unless the path is
/images/
/html_pages/
/secret_hiding_place/
Any ideas?
Also have to be careful for 301 loops.
Thanks for any clues or ideas.
THANKS!!!
J

Redirect for path but only for specific domain

We have different domains that are hosted on our server. Recently one of the main sections of our site has been moved to another server and been given a subdomain:
http://www.mysite.com/store
Has been moved to
http://store.mysite.com
Within our apache VirtualHost we wanted to redirect all traffic from the old domain to the new one:
Redirect permanent /store http://store.mysite.com
The problem is, we have other hosted sites that are being redirected now:
http://www.othersite.com/store
http://api.greatsite.com/store
We don't want this. How can I only have apache do redirects if http://www.mysite.com/store which has the /store path, and ignore other domains with /store?
Use mod_rewrite based code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^/?store(/.*|)$ http://store.mysite.com [L,R=301,NC]

How can I redirect all traffic from one domain to another with an .htaccess file?

Say I have a subdomain xxx.yyy.com running Apache. The files are stored in /home/someone/public_html/xxx.
What I want to do is redirect all requests to a domain name zzz.com which is using the same location for its files. (In other words, xxx.yyy.com and zzz.com are aliases for each other)
I just want people accessing zzz.com, so if someone goes to xxx.yyy.com they should be redirected to zzz.com.
Can this easily be done with a rewrite rule in an .htaccess file?
Try
RewriteCond %{HTTP_HOST} ^xxx\.yyy\.com
RewriteRule (.*) http://zzz.com/$1 [R=permanent,QSA,L]