Apache .htaccess rewrite rule causing an invalid host name? - apache

I'm trying to forward all requests from domain1.com to domain2.com, without redirect using mod_proxy and mod_rewrite. My .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^(.*)$ http://domain2.com/$1 [L,P]
Actually this isn't working because it produces an invalid host. The target site domain2.com complains about invalid host, that it seems to be the following:
domain1.com, domain1.com
Yes, with comma. Any idea is much appreciated.
If I dump the server variables i get domain1.com, domain1.com as SERVER_NAME and HTTP_HOST.
EDIT: I solved looking at the source code doing the check.
AFIK it seems pretty normal to double the host value. Why? Because setting the IP of the proxy as trusted proxy the magic happens: the host get splitted by ',' and the last will be used as host, that is domain1.com. It works.

Sending requests from one domain to another is a perfect example for ProxyPass.
ProxyPreserveHost On
ProxyPass / http://domain2.com
ProxyPassReverse / http://domain2.com
This requires the modules proxy and proxy_http, but should solve your problem.

Related

Redirect non-www to www https Apache XAMPP

Please forgive what may sound like a similar question to what has been asked, however, I am VERY new to XAMPP and Apache and I have tried all possible combinations of the Rewrite Rules mentioned in other threads, which I have placed in the httpd.conf, httpd-default.conf and also in .htaccess and I simply cannot get the rewrite rules to work.
I simply want to redirect example.com to www.example.com. Please note that my redirect from HTTP to HTTPS is working 100%, so if someone can please advise on exactly where I should place the rewrite rule, and which one to use, to force non-www to www, I would be most appreciative.
I have full access to the server so I can edit either .conf or .htaccess files. Also I have checked and the part in httpd.conf that allows overrides to read the .htaccess files is enabled. The .htaccess file is currently in the same folder where my website lies. Just to add, I don't get any error, it just says the page has timed out if I type in example.com, if I type www.example.com then the page loads as expected.
Try this in your .htaccess file:
Replace example.com with your url.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
The connection has timed out
This is a DNS or server config error. The request is not even reaching your server, or your server is not responding to the request. (So any attempt to implement a redirect will naturally fail.)
A quick check of the DNS for example.com and www.example.com shows they are pointing to different IP addresses (you have an A record configured for each). The example.com host is 1 digit different and would appear to be incorrect (the IP address does not respond to requests, hence the "time out").
Instead of having a separate A record for each hostname. If the www subdomain is supposed to point to the same site as the domain apex (ie. example.com) then configure a CNAME record instead that points www.example.com to example.com (this is not a "redirect").
You then need to ensure that your server will accept requests to the non-www hostname. For example:
ServerName example.com
ServerAlias www.example.com
(It's possible you have the ServerName and ServerAlias reversed - which is "OK".)
And the SSL cert needs to cover the non-www hostname (it does).
You can then implement your non-www to www HTTP redirect as mentioned.

redirect from subdomain to different host without 301

I need to point from someSub.somedomain.com to mysub.mydomain.com, and I need the url of the site to continue reading someSub.somedomain.com
I've tried many variations of:
RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]
But I can't seem to get anything to work. Any advice?
This answer kind of already answers this question:
https://serverfault.com/questions/506623/masking-the-url-in-a-mod-rewrite
But it appears to me what you are looking for is a reverse proxy.
In the virtual host for your server:
ServerName somesub.somedomain.com
ProxyPass "/" http://mysub.mydomain.com/
ProxyPassReverse "/" http://mysub.somedomain.com/
Now when you go to your http://somesub.somedomain.com all the requests will behind the scenes actually be going to http://mysub.mydomain.com but the browser user won't see that.
There are various customizations to this you can make.
Don't forget to enable mod_proxy

Redirection https to https within same Apache

I have a requirement where I have to redirect my hostname to particular application which is again hosted on same Apache. Let's take an example, When I hit on host(https://domain1.example.com), It should internally redirect me to Apache Web Application (https://domain1.example.com/application1) without changing the browser URL.
I am not sure how to achieve SSL to SSL redirection. Thanks in Advance..!!!
This should work. This will redirect all incoming urls that are going to domain1.example.com/ to domain1.example.com/application1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.example.com$
RewriteRule ^$ https://domain1.example.com/application1 [L,R=301]
If without changing browsing URL is your goal then PROXY is your way.
Put following in your apache vhost or global file,
ProxyPass https://domain1.example.com/ https://domain1.example.com/application1
ProxyPassReverse https://domain1.example.com/ https://domain1.example.com/application1
PS shibboleth has nothing to do with this, at least you have not mentioned any case.
EDIT
ProxyPass should come to virtural host not in location
Ideally all the location tag should be out of virtual host

Apache redirect after a rewrite

I'm trying to figure out how to properly do this. I'm hosting a domain that used to have a website also on the same server, however the website has now been moved to a different machine, but they want to keep the domain hosted on our DNS. Rather than changing the DNS record right now, I'm trying to figure out how to do a proxy redirect but I'm having some trouble.
Right now, I'm using the RewriteEngine to rewrite the URL as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.org [NC]
RewriteRule ^/(.*) http://www.domain.org/$1 [L,R]
This is in case someone looks up the website simply by http://domain.org it will get rewritten to http://www.domain.com - that works fine.
Now I need to redirect it to go to an IP address with a username:
http://111.222.333.444/~user
Rather than simply redirecting it to that address, I'd like to do a proxy where the domain will still be visible in the browser's address bar, while also keeping the above rule in place.
Suggestions anyone?
Make sure mod_proxy is enabled and do:
<VirtualHost *:80>
ServerName www.domain.com
ProxyPass / http://111.222.333.444/~user
</VirtualHost>

How to rewrite urls that go through apaches mod_proxy

I have configured my apache 2.2 server as a simple forward proxy using mod_proxy and mod_proxy_http.
When the client requests a URL of the following format:
http://specific.host.com/specific?specificarg1=(.+)&specificarg2=(.+)&specificarg3=specificvalue
to be requested in a rewritten form where the values for specificarg1 and specificarg2 get replaced by values defined in my server config.
It would be also possible not to use regex but to rewrite a specific url to another specific url, but i would prefer with regex matching.
So after reading the documentsations for mod_rewrite, mod_proxy and so I tried something like the following to get some sort of rewrite working at all:
RewriteRule .* http://www.google.com/ [P,L]
just like that in the server config, nothing gets rewriten when surfing over the proxy
<VirtualHost *:80>
ServerName domain-i-tried-to-surf-to.com
`RewriteRule .* http://www.google.com/ [P,L]
</VirtualHost>
no luck either
ProxyPass(Reverse) / http://www.google.com
ProxyPass(Reverse) /path/i/tried/to/surf/to http://www.google.com
no luck with that too
ProxyRemote * http://www.google.com
no luck as well
I also tried to put rewrite rules into proxymatch directives but I am just unable to rewrite a url. Can somebody point me in the right direction?
You first need to add a RewriteEngine on statement at the very beginning to even get mod_rewrite to process any rules.