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
Related
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.
My question is regarding RewriteMap on apache2: I want to apply a rewrite condition, so that all request on my proxy are proxied to an completely new domain.
Eg localhost/test or any other url should just go to www.mydomain.com:
RewriteRule / http://www.mydomain.com [P]
Works fine. If I access localhost, I still see "localhost" in my browser address line, but mydomain.com is presented. BUT if I now click on any link on this mydomain site, I will get a "Not Found" response.
The sourecode of mydomain contains eg this link:
Link
If I access the site in a normal way, this would result in: www.mydomain.com/lab/sale.php, and works fine.
If I access the site through my proxy and the rewriteRule takes place, I would after the link click be directed to: localhost/lab/sale.php, which does not exist of course.
Question: how can I a user that accesses the site through my proxy browse on the whole site as if he would really access this site?
The RewriteRule directive isn't like a ProxyPass or Redirect where they essentially link 2 nodes together and everything following it also gets proxied. The rule that you have only proxies the request URI /, not /lab/ or /etc.php or anything else. You need to create a match and pass that along as a backreference:
RewriteRule ^/?(.*)$ http://www.mydomain.com/$1 [P]
Or you can use the %{REQUEST_URI} variable:
RewriteRule ^ http://www.mydomain.com%{REQUEST_URI} [P]
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.
Is it possible use mod_rewrite to resolve addresses hosted on another server?
Say I want to setup this URL:
http://www.myserver.com/myfolder/
To actually resolve to:
http://www.anotherserver.com/anotherfolder/
If so, could you provide a RewriteRule example?
You can use the P flag in a mod_rewrite rule to get that substitution URL requested by mod_proxy:
RewriteEngine on
RewriteRule ^myfolder/$ http://other.example.com/anotherfolder/ [P]
Now when a client is requesting /myfolder/ from your server, it will request http://other.example.com/anotherfolder/ and send the response from that server back to the client.
No, tunneling is not possible, you'd have to use a CGI script for this. However, you can redirect:
RewriteRule ^(.*) http://new.example.com/$1
with or without the [R] flag, and it will automatically redirect the user to the new domain.
Edit: Apparently it is possible to tunnel requests with mod_proxy and the [P] flag. See Gumbo’s answer.
Hey I tried rewrite and didn't work (just tested in localhost)
Also I find this easy way
Redirect /myfolder external_url
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/