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

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]

Related

Point add-on domain to a subfolder and redirect direct access to that subfolder to add-on domain using .htaccess

I have a site example.com/main and another site example2.com.
I have set up an add-on domain for example2.com which shows files in the main subfolder in the example.com domain. What I want is that when users go to example2.com, they REMAIN on example2.com while they see files from example.com/main. So if they access other files like main/contact.php, it shows as example2.com/contact.php.
I have successfully done what I mentioned above in the add-on page of cPanel.
My issue:
I want it so that when users visit example.com/main directly, I want them to be redirected to example2.com and then see the main subfolder. I have had no luck as I keep getting stuck in a loop.
Doing Redirect 301 / http://example2.com/ doesn't work.
Thank you.
You can use:
RewriteCond %{HTTP_HOST} example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+main[\s/?] [NC]
RewriteRule ^main(/.*)?$ http://example2.com$1 [NC,R=301,L]

How to have mod_rewrite validate cookie from different server, same domain

I have two server, foo.example.com and bar.example.com. They are on two different servers but on the same domain. On foo.example.com I have the page on there create a cookie using
setcookie('name','value',time()+3600,'/','example.com',1)
and it gets created just as expected.
On bar.example.com I need to restrict access to only those with that cookie and if it's not there to redirect to a custom 403 page that tells the user they need to log into foo.example.com. This is the rewrite rule I have.
RewriteCond %{HTTP_COOKIE} !name=value [NC]
RewriteCond %{REQUEST_URI} !^/error/403.html
RewriteRule ^(.*)$ /error/403.html [R,NC,L]
But I keep on getting the 403 page even though the cookie exists and has the correct domain listed and values are correct.
What is wrong with this code? Is what I'm trying to do even possible?

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]

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

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

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]