How to rewrite urls that go through apaches mod_proxy - apache

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.

Related

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

Apache .htaccess rewrite rule causing an invalid host name?

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.

Apache Rewrite rule with decode url

I have a basic rewrite rule
RewriteRule ^/v1/(.*)$ http://127.0.0.1:8000/v1/$1 [P,L,QSA]
Rewrite is working fine, but I have a issue with a request which is coming to the apache.
https://example.com/v1/test/bd95f52d72942198eb84e1b6998a6259/phone_numbers/%2B61180087654
Apache is decoding this URL to
https://example.com/v1/test/bd95f52d72942198eb84e1b6998a6259/phone_numbers/+61180087654
I want apache should pass the same request to my code. I don't want + with number. What should I change to get the expected result.
Thanks
Unless you have some other reason to use rewrite, try this instead of RewriteRule ...
ProxyPass /v1/ http://127.0.0.1:8000/v1/ nocanon
ProxyPassReverse /v1/ http://127.0.0.1:8000/v1/
(ProxyPass should also have performance benefits over rewrite, in some cases)

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>

Configuring mod_rewrite and mod_jk for Apache 2.2 and JBoss 4.2.3

My problem is as follows: I have JBoss 4.2.3 application server with AJP 1.3 connector running on one host under Windows (192.168.1.2 for my test environment) and Apache 2.2.14 running on another FreeBSD box (192.168.1.10). Apache acts as a "front gate" for all requests and sends them to JBoss via mod_jk. Everything was working fine until I had to do some SEO optimizations. These optimizations include SEF urls, so i decided to use mod_rewrite for Apache to alter requests before they are sent to JBoss. Basically, I nedd to implement 2 rules:
Redirect old rules like "http://hostname/directory/" to "http://hostname/" with permanent redirect
Forward urls like "http://hostname/wtf/123/" to "http://hostname/wtf/view.htm?id=123" so that end user doesn't see the "ugly" URL (the actual rewrite).
Here is my Apache config for test virtual host:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/usr/local/www/dummy"
ServerName 192.168.1.10
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule /directory/(.*) /$1 [R=permanent,L]
RewriteRule ^/([^/]+)/([0-9]+)/?$ /$1/view.htm?id=$2
</IfModule>
JkMount /* jsp-hostname
ErrorLog "/var/log/dummy-host.example.com-error_log"
CustomLog "/var/log/dummy-host.example.com-access_log" common
</VirtualHost>
The problem is that second rewrite rule doesn't work. Requests slip through to JBoss unchanged, so I get Tomcat 404 error. But if I add redirect flag to the second rule like
RewriteRule ^/([^/]+)/([0-9]+)/?$ /$1/view.htm?id=$2 [R,L]
it works like a charm. But redirect is not what I need here :) . I suspect that the problem is that requests are forwarded to the another host (192.168.1.2), but I really don't have any idea on how to make it work. Any help would be appreciated :)
The reason your second rewrite rule doesn't work is that you use the '?' in your URI definition and the URI definition never contains the separator '?'.
So simply use the rewrite rules without it. eg.
RewriteRule ^/([^/]+)/([0-9]+)/$ /$1/view.htm?id=$2 [R,L]
Simply, doesn't works because the first RewriteRule has the [L] at the end, which means is the last rule to process.