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
Related
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]
#add in the www
RewriteCond Host: (?:http\://)?drink-superstore\.com
RewriteRule (.+) http\://www.drink-superstore.com$1 [I,RP]
We have it in one of our sites and I need it in another but I did not want to just use it without understanding it.
Help appreciated.
The RewriteCond is a condition, like an IF statement. It tests to see if the HOST is drink-superstore.com, with no leading www. If the condition evaluates to true, then the rule is applied for the request.
The rule says: Redirect to the same url ($1), using a different host, with the www prefix. The redirect comes in with the RP option, which stands for "Redirect Permanently". That is an HTTP 301 response code.
More information on ISAPI rewrite can be found here. I learned how to use URL rewrites from this handy page.Its part of the official Apache documentation.But also here is a tutorial for beginners on URL rewriting. While its geared towards apache, This should at least provide somewhere to start.
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
For example, one instance of apache is managing
www.site.com/folder1/a.html
www.site.com/folder2/b.html
www.site.com/folder3/c.html
I need to make sure that access to
www.site.com/folder3/c.html
is https only.
All these folders are in the same document root.
Is this possible? If not, what you recommend as the minimum changes necessary in order to get what I want?
I believe you can do something in the Apache configuration like:
RedirectMatch /folder3/(.*) https://www.site.com/folder3/$1
Not sure if that's proper though.
Hope that helps...
You can try this solution:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/folder3/c.html$ https://www.site.com/folder3/c.html$1 [R,L]
With this solution you can also define which kind of redirection to apply. And, the most important, the rule will only run when in HTTP scheme.
I would like to redirect/rewrite this two kinds of URLs:
mydomain.com -> newdomain.com
mydomain.com/specificPage -> newdomain.com/newSpecificPage
mydomain.com/anyOtherPage -> mydomain.com/anyOtherPage (no redirect here)
So I just want to redirect the root domain to a new domain, and some pages from my domain to some pages on a new domain...
How can I do that on a JBoss server ?
Have you looked into http://www.jboss.org/jbossweb/modules/rewrite.html? It looks like what you're looking for, and it's pretty similar to Mod_rewrite for Apache.
You might take a look at this http://code.google.com/p/urlrewritefilter/
Sounds like you want to send an HTTP 301 Moved Permanently response.
RewriteCond %{REQUEST_URI} ^URI_TO_REDIRECT
RewriteRule redirect=301 NEW_SITE [L]
or similar. The [L] is to tell it to redirect immediately instead of continuing to rewrite.
If you are routing through apache at all it is possible to use mod_rewrite; you just need to be careful as to where you declare the rewrite rules. Directory configs and .htaccess files won't work; you need it as a global configuration for the entire host. Similar thread on serverfault.