apache rewrite rules are not working after adding proxy pass - mod-rewrite

i am trying to rewrite the url in apache which internally redirect the requests to apache tomacat
Here is my httpd.conf code
<IfModule mod_rewrite.c>
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
ProxyPass / http://localhost:8080/myapp/my.html
ProxyPassReverse / http://localhost:8080/myapp/my.html
RewriteEngine on
RewriteRule ^/(.*)/$ http://localhost:8080/myapp/my.html?product=$1 [QSA]
</IfModule>
so basically what i am trying to do is if i enter localhost/myapp then it should redirect me to localhost:8080/myapp/my.html
Next is if i enter the url localhost/myapp/8 it should redirect internally to localhost:8080/myapp/my.html?product=8.
Now the problem is ProxyPass is working absolutely fine. But the rewrite rule is showing 404 error.
If i remove the ProxyPass code then the same rewrite rule works but it shows the modified url in the browser.
So i want to know where should i place RewriteRule to make it work with ProxyPass and y the rewrite rule is showing the modified urls?

You need to add the [P] flag to the RewriteRule. This causes the RewriteRule to 'proxy' the same way your ProxyPass directive does. At the moment your rule doesn't make any sense. Alternatively you could do something like:
RewriteRule ^/(.*)/$ /myapp/my.html?product=$1 [QSA,PT]
Which should cause the URL to be rewritten and then passed through (this happens because of the PT flag) to any remaining modules that need to process the URI path, in this case the proxy module.
FYI the terminology is wrong, when you say if i enter localhost/myapp then it should redirect me to localhost:8080/myapp/my.html you really mean if i enter localhost/myapp then it should proxy to localhost:8080/myapp/my.html. A redirect is an external response which cases the browser to request a new URL and the text in the browsers address bar will change.
Mind you, with your current configuration, requesting localhost/ will proxy to localhost:8080/myapp/my.html. So if you can specify which is correct it would help.

Related

Conditional reverse proxy in apache

if request comes from public ip 50.12.95.78. Proxypass to http://192.168.1.100/quote url.
if request comes from other public ip . Proxypass to http://192.168.1.100/ url
What is the settings for it in apache? Reverse proxy wont work in if condition.
You can set up a conditional reverse proxy in Apache httpd by utilizing mod_proxy and mod_rewrite.
For instance, if you originally had your reverse proxy set up with the following configuration:
ProxyPass / http://192.168.1.100/
ProxyPassReverse / http://192.168.1.100/
It can be made conditional by using the proxy flag on a RewriteRule. An example configuration could look like this:
RewriteEngine On
ProxyPassInterpolateEnv On
RewriteCond "%{REMOTE_ADDR}" =50.12.95.78
RewriteRule (.*) http://192.168.1.100/quote$1 [P,E=proxy_pass_path:/quote]
RewriteRule (.*) http://192.168.1.100$1 [P]
ProxyPassReverse / http://192.168.1.100${proxy_pass_path}/ interpolate
The first RewriteRule is applied and proxied only if the preceding
RewriteCond directive is matched, i.e. if the Remote IP
Address is 50.12.95.78.
The [P]roxy flag also prevents further RewriteRule directives from being applied.
The environmental variable proxy_pass_path is set to tell the ProxyPathReverse directive that the path has been modified.
The second RewriteRule
is automatically applied if the first did not evaluate.
Finally, the
ProxyPathReverse directive is modified to rewrite response headers
based on the proxy_pass_path variable.
Going beyond the scope of your question, bear in mind that this kind of proxy will not implement any real form of security. IP addresses other than 50.12.95.78 will still be able to access http://192.168.1.100/quote simply by requesting http://<Proxy_Host>/quote.

redirect subdomain to page unless other page is called

i am trying to redirect all subdomains with more than 2 letters :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^\.]{2,})\.domain\.com$ [NC]
RewriteRule .* index.php?page=static&subject=%1 [L,QSA]
for example : about.domain.com to display content of domain.com/?page=static&subject=about
but the URL remains about.domain.com in the browser
as well as if you're on about.domain.com and requested another page on that domain ( all pages go through index.php?page=blablabla) you need to be redirected back to domain.com/(whatever requested)
Same-domain internal rewrites can be done with the [P] (proxy) option.
If you need to reverse proxy a URL from the same virtual host config, you might be able to get away with a [P] rule, but if the domain of the internal request is from a different virtual host (or on a different box entirely) you probably need to look at a ProxyPass config.
In either case you will need mod_proxy installed and enabled.

mod_rewrite returns 302 found

I have an apache 2.2 and I am using mod_rewrite. My objective is to forward traffic from
http://localhost:80/AA to http://localhost:8090/BB. So I have a simple rule
RewriteRule http://localhost:80/AA http://localhost:8090/BB
My problem is that the client receives "302 Found". I was hoping that the RewriteRule will
forward the traffic from AA to BB and finally the BB will send the response to my client.
Is there a problem with my configuration?
See the documentation for RewriteRule
What you wanna do is add the R flag to your rule, for redirection.
RewriteRule http://localhost:80/AA http://localhost:8090/BB [R=permanent,L]
L for last rule.
The left side, or pattern, of your RewriteRule should just be the filename you want to match, e.g. ^/AA$. If you also want to make sure the host and port match specific values, you should set up RewriteCond's to match %{HTTP_HOST} and %{SERVER_PORT}. However the latter may be unnecessary unless your web server is configured for multiple domains and ports.
This is untested, but something like this:
RewriteCond %{HTTP_HOST} ^localhost
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/AA$ http://localhost:8090/BB
I would recommend that you try just this first:
RewriteRule ^/AA$ http://localhost:8090/BB
and add the RewriteCond's if you need them.
Edit based on comments: If you are trying to avoid the "302" response, you cannot do that for the situation you describe. When you use mod_rewrite to redirect from one host to another, you will get two responses sent to your browser. The first is 302, which tells the browser to go to the second URL. The second response should be 200.
With mod_rewrite, you can avoid the redirect in the middle if the rewrite is from one page to another on the same server and port. In that case, the rewrite is internal and the web server can respond with the BB page even though the request is for AA. If you use a different server or port, the web server for AA does not have access to the page BB, so it responds with a redirect to the server that does have access.
If you can reconfigure your site to use the same port for AA and BB, you can make it work. If not, I think you might be able to do what you want with mod_proxy. I have never used mod_proxy so I am not sure what you would need to do.
If you wish to forward traffic, I guess mod_rewrite can't do that. You should preferably use mod_proxy: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypassreverse
Example from the documentation:
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar

RewriteRule without changing the address in the browser

I'm trying to make an address redirect to another one without actually changing the URL in the browser address bar.
User go to /path/page.php and see what is displayed on /path/index.php.
In the address bar the URL remains /path/page.php.
This is the code for the redirection:
Options +FollowSymLinks
RewriteEngine On
RewriteRule page.php index.php [NC]
I'm wondering if I need to use [P] for this task :/
[P] is a proxy flag, you need it only for silent redirection to another domain or web-server (in this case Apache works like HTTP proxy). In your case page.php and index.php reside on the same server, so [P] flag is useless.

mod_rewrite acting like redirect to another domain? How do I make a rewrite?

I want to redirect www.mydomain.com/store to http://store.anotherdomain.com/me
When I use RewriteRule ^store$ http://store.anotherdomain.com/me it ends up redirecting, meaning the URL changes, rather than remaining www.mydomain.com/store
What do I need it to do to do the rewrite properly?
When I use RewriteRule ^next$ /mydomain/subfolder/subfolder/subfolder is seems to work fine.
RewriteRule ^store$ http://store.anotherdomain.com/me [P]
Note the [P] at the end. You'll also need to have the mod_proxy module enabled.
You don't need mod_rewrite at all.
You need to enable mod_proxy and configure a reverse proxy. You can even pass cookies from the other domain and make them look as if they were from your site.
ProxyPass /store/ http://store.anotherdomain.com/me/
ProxyPassReverse /store/ http://store.anotherdomain.com/me/
ProxyPassReverseCookieDomain store.anotherdomain.com www.mydomain.com
ProxyPassReverseCookiePath /me/ /store/

Resources