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

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]

Related

How to add a rewrite rule for domain and it's subdomains in Plesk 11.0.9?

I need to redirect a request file from all possible subdomains to one file which is located in top level domain:
^.example.com/robots.txt >> http://example.com/robots.txt
The robots.txt may or may not exist in the subdomain httpdocs, this rule must be exucuted first (we want to block all crawlers in advance).
The folder structure provided by Plesk 11.0.9 is:
example.com/
...
httpdocs/
subdomains/
sub1/
httpdocs/
example.com is owned by root.
Where can I add an apache rewrite rule that would be trigger for all subdomains?
Edit: I tested with ping doesntexist.example.com and the request does get directed to example.com which means teoretically there should be a point in processing to execute the rewrite logic.
Try placing this rule in /example.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(example\.com)$ [NC]
RewriteRule ^robots\.txt$ http://%1/$0 [L,R=302,NC]

Apache mod_rewrite - want a file system redirect not a 301 redirect

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

Blocking access to a folder through http

I hope this question is not redundant, but I could not find any answer to the exact question I am asking.
I own a multiple site shared hosting plan on a Linux server.
The hosting plan refers to a domain that we can call www.domain1.com
Every other domain is hosted as a subdir of the main domain. so for example domain2.com will be hosted like: www.domain1.com/domain2.com/
It could be reached using that path or using www.domain2.com directly.
I want to disable the access to domain2.com as subfolder of domain1.com.
I suppose I need to put a .htaccess file in every subfolder of domain1.com which is hosting a website. Am I correct? What do I have to put in the .htaccess file?
thank you in advance,
Max
Put these rules in the htaccess file in domain1's document root (the parent folder):
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1\.com$ [NC]
RewriteRule ^(domain2\.com|domain3\.com)/ - [L,F]
Where the domain2\.com etc are the subfolders that you don't want to be able to have direct access.
However, if you have rewrite rules in each of those subfolders, then you do need to add rules in each of the subfolders:
RewriteCond %{HTTP_HOST} domain1\.com$ [NC]
RewriteRule - ^ [L,F]
The F flag will cause the request to return a 403 Forbidden.

Apache Rewrite: secondary htaccess for domain specific RedirectMatch

On shared web-hosting my software supports multiple domains (all domains point to the same public_html root directory).
What I want to do is keep redirects (and any RedirectMatch) in their own host specific/dedicated .htaccess file.
Visually the directory structure looks like this...
/public_html/ (all domains are pointed internally to this directory)
/public_html/.htaccess
/public_html/www.example1.com/
/public_html/www.example2.com/
/public_html/www.example3.com/
There are two approaches I'm considering though would appreciate input from others:
The first would be to keep domain specific redirects out of the main .htaccess file as defined above. So I'd like to have redirects handled by the .htaccess files as defined by below if possible...
/public_html/www.example1.com/.htaccess
/public_html/www.example2.com/.htaccess
/public_html/www.example3.com/.htaccess
...if this is not feasible I'll settle for a rewrite to a PHP file to hand off redirects to PHP instead. I imagine this isn't as performance oriented though on the other hand it would give me the opportunity to log redirects and see how long it takes them to level off.
Some clarifications:
I'm using shared web hosting so anything Apache related needs to be done through .htaccess files only.
There are no redirects/matches in the master .htaccess file nor will there ever be since two domains may eventually attempt to use the same redirect.
Since you are on shared host, You cannot afford to have any solutions concerning conf files (which BTW are better). So wont bother to list them. Best way to do the above is like this:
The code was written keeping in mind that none of the domains share any kind of file/data on the server. Every file/data pertaining to a domain is kept under a folder having the name equal to its domainname.
The code below is tested(both static and non static):
RewritEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
And add either of the following to the above:
for doing it statically:
RewriteCond %{HTTP_HOST} ^www\.(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /www.%1%2/$1 [L]
for doing it statically: and also if you want to access the site without www
RewriteCond %{HTTP_HOST} ^(www\.)?(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /%1%2%3/$1 [L]
for Non-statically do it: this is a better sol
RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [L]
All the above will do is redirect URI to their specific domain's folder. All other domain specific rewrites can be handled in the respective folders.
If you have URIs without the www, i.e. example1.com change ^www\.(example1|example2|example3)(\.com)$ to ^(www\.)?(example1|example2|example3)(\.com)$

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