Forward multiple domain names to multiple sites - apache

Have searched and viewed many threads, but mostly are about forwarding multiple domains to 1 single site/domain.
What i have now is around 50 domainnames which i have directed to my server ip address.
On my server i use Directadmin.
What i would like is to have the most efficient/easiest way to manage these domainnames and have them forward to (different) sites.
So for example,
Domain1.com, needs to be forwarded to someotherdomain1.com
Domain2.com, needs to be forwarded to someotherdomain2.com
Domain3.com, needs to be forwarded to someotherdomain3.com
What would be the easiest way for me to set something like this up?
Thank you in advance

You can use these type of rules:
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain1\.com$ [NC]
RewriteRule ^ http://someotherdomain1.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain2\.com$ [NC]
RewriteRule ^ http://someotherdomain2.com%{REQUEST_URI} [R=301,L,NE]
Alternatively if you have access to Apache config then you can utilize RewriteMap feature and have just one single rule here like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.com$ [NC]
RewriteRule ^ http://${domainMap:%1}.com%{REQUEST_URI} [R=301,L,NE]
In your Apache config you need to define domainMap as:
RewriteMap domainMap txt://path/to/domain-map.txt

Related

htaccess redirecting multiple domains, https and http, www and non-www, all to one https domain

I have three different domains, and I want to make sure that all domains redirect to the one domain.
I want to make sure all cases are covered: if the user types http or https, if they include www or not.
This create 12 different possibilities...
http://www.domain1.co.uk
https://www.domain1.co.uk
http://domain1.co.uk
https://domain1.co.uk
http://www.domain2.uk
https://www.domain2.uk
http://domain2.uk
https://domain2.uk
http://www.domain3.co.uk
https://www.domain3.co.uk
http://domain3.co.uk
https://domain3.co.uk
And I need all of these to redirect to:
https://www.domain3.co.uk
I have set up my .htaccess file in the way I thought should work with most variations, and I have researched this on Google and SO, and this looked to me to be the best solution:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain3\.co.uk$ [NC]
RewriteRule ^ https://www.domain3.co.uk%{REQUEST_URI} [R=301,L]
But this covers only 11 of these variations.
With this htaccess file, if I visit the URL:
http://www.domain3.co.uk
It does not redirect.
All other variations redirect as required.
How can I change my htaccess file so that all 12 possible variations are covered?
You can use this single rule with a [OR] clause:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.domain3\.co.uk$ [NC]
RewriteRule ^ https://www.domain3.co.uk%{REQUEST_URI} [R=301,L,NE]

Conditionally whitelist IP's in Apache using presence or absence of files

I found a pretty good solution to this problem but it is missing one component that I need. This is close:
# WHITELIST IPS #
RewriteMap ipslist txt:/path/to/whitelist.txt
RewriteCond %{REMOTE_ADDR} ^(.*)$
RewriteCond ${ipslist:%1|black} ^black$ [NC]
RewriteRule (.*) - [F]
MOST of the time I want to limit access to the site, but there will be times when I want many (beta testers) to have access. At that point, it will be easiest just to let anyone access the site during the test. So what I would really like to do is to make the whitelist apply conditionally. IF the whitelist.txt file is present, limit the access. But if there is no file, then let anyone access the site. Or, find some other approach.
I have searched for something similar but what I have above is as close as I can get. Can I use *.*.*.* in the whitelist.txt to allow ALL IP's when I need to? Or is there some better way to "turn on and turn off" the IP restriction logic?
Have you considered using a flag file as a condition instead of the file of ip addresses?
Something like:
# WHITELIST IPS #
RewriteMap ipslist txt:/path/to/whitelist.txt
RewriteCond /path/to/flag_enabled_feature_file -f
RewriteCond %{REMOTE_ADDR} ^(.*)$
RewriteCond ${ipslist:%1|black} ^black$ [NC]
RewriteRule (.*) - [F]

redirect url from Domain A to another but applications exist on Domain A only

I have requirement on OHS server(apache) to redirect all requests from domain A to domain B but the pages needs to be rendered from domain A only. Below configuration doesn't seems to work.
RewriteCond %{HTTP_HOST} ^http://portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [R=301,L]
Am i doing anything wrong?
For domain redirects could you simply point your new domain B to A using either CNAME or A records in the domain registrar?
There are several issues. You can't do a 301 redirect and expect to stay on Domain A. One way you might be able to get this to work is the use of mod_proxy and the P flag. Also the http_host variable only matches the hostname.
You need to make sure mod_proxy is enabled and you can try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [P]
In this way your domain A is sending a redirect to user browser, so the page is going to be provided from domain B.
If you have to make domain A providing pages from domain B, but user do not have to see domain B, you may check Proxy and/or ProxyPass rule from mod_proxy.
One more thing, if you proxy domain B thru domain A, you may have to rewrite inpage links, so take a look at ProxyHTMLEnable and ProxyHTMLURLMap
Dot (.) is a special character in the RewriteCond pattern, Escape it using a backslash
Try this
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.portal\.mycompany\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ http://newportal.mycompany.com:85/$1 [R=301,QSA,NC,L]

Generlized IP Canonicalization Solution Within .htaccess

I am looking for a generalized IP canonicalization solution that would not hard code IP address in my htaccess file. I generally run my applications on amazon EC2 instances and IP addresses frequently change. I'd hate to have to manually update htaccess every time it does so. So specifically, I am looking for htaccess rules that would match pages accessed via ip address and perform a 301 redirect to the actual domain.
Had you considered using some variety of Dynamic DNS instead? That would let the IP addresses change frequently while maintaining the same DNS names.
I’m going to assume you don’t have any subdomains. The following should do the trick:
Options +FollowSymLinks
RewriteEngine on
# 1
RewriteCond %{HTTP_HOST} .
# 2
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
# 3
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This will redirect requests for anything1 other than the actual domain2 for all resources3.
EDIT: For subdomains, then the following should work:
# If it's one of the domains of the IP address …
RewriteCond %{HTTP_HOST} ^domain\.com [OR]
RewriteCond %{HTTP_HOST} ^111\.222\.222\.111
# … then redirect request for all resources.
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

How to rewrite URL based on domain in apache and add extra parameter?

Basically, what I want to do is to rewrite all urls because we have many different languages. We have a server that hosts several domains. We have www.example.com, www.example.fr, www.example.de, www.anotherdomain.com, www.anotherdomain.de. What I want to do is to redirect all requests from example.xxx to www.example.com with extra url parameter lang=en. This should not affect other domains like www.anotherexample.com etc.
This does not work:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^(.*)$ http://www.example.com/$1?lang=de [PT]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^(.*)$ http://www.example.com/$1?lang=fr [PT]
One thing that makes it even more difficult is that the ServerName is totally different than the host name, it is called prod.migr.com.
Any suggestions would be appreciated.
Try this:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^ http://www.example.com%{REQUEST_URI}?lang=de [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^ http://www.example.com%{REQUEST_URI}?lang=fr [L,R=301,QSA]
The PT flag is most likely your problem. I've never seen it used when the target is a full domain address because it's meant for URI's to be further redirected with mod_alias.
The flag you should be using is the QSA flag in case the page the user is visiting has a query string on it.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^(.*)$ http://www.example.com/$1?lang=de [QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^(.*)$ http://www.example.com/$1?lang=fr [QSA]
However, a much better solution would be to check the host the user is visiting in your server-side language such as php or asp if all languages are hosted on the same server like this.
EDIT in response to additional information:
You can not get POST variables through rewriting to different domains like that because it has to redirect the request.
Your best bet is to determine the language in your server side language instead of using mod_rewrite.
If you use php it would be like this
$lang = substr(strrchr($_SERVER['HTTP_HOST'], '.'), 1);
Other languages have similar ways to determine the host.