Re: best way to modify a URL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Much, much better, and really clear this time.

Oliver Schoenborn wrote:
Sorry I didn't notice the html format, I'm amazed that some people were able to understand my question!
(only the smart ones)
 Hopefully it works now. Here is what I am trying to do, 2 separate cases:

1. Client goes to http://dodo.foo.com: A. server changes this to http://foo.com/bar
  B. server returns content (which will be http://foo.com/bar/index.html)
  C. client sees the content in browser
  D. AND the client's browser shows the URL (in Location text field) as being http://dodo.foo.com (rather than http://foo.com/bar/index.html)


As someone else indicated previously, this is a standard feature of mod_proxy. First, let's assume for a moment that "dodo.foo.com" and "foo.com" are really two different machines, with each its own Apache on it.

If you want to use mod_proxy, use ProxyPass to forward the call to the backend server.

ProxyPass / http://foo.com/bar/

The client sees only the front-end server, so he should never see the URL of the back-end.
However, there are exceptions :

a) if the back-end server itself sends a re-direct response (to the front-end server, but passed by the front-end server back to the client), then normally that re-direct response will refer to the back-end server (foo.com). But luckily, that's what the proxyPassReverse directive is for, to correct that situation.
So, if you do :

ProxyPass / http://foo.com/bar/
ProxyPassReverse / http://foo.com.bar/

that would work.
(However, this is all a bit silly, because it means that you have one front-end system that does nothing else than passing everything to the back-end and back; so why have a front-end ?)

and b) for links inside of html pages. That is your case below.


2. Client clicks on a link that is on the page just received:
  A. the href will be, say, http://foo.com/bar/someotherpage.html
  B. server returns content
  C. client sees the content in browser
  D. AND the client's browser shows the URL (in Location text field) as being http://dodo.foo.com/someotherpage.html (rather than http://foo.com/bar/someotherpage.html)


(First note that what you want above is not really possible. If the link in the page that the client gets is really "http://foo.com/bar/someotherpage.html";, then that is what the client will see in the URL bar after he clicks on it). (Unless you want to have "foo.bar" redirect to "dodo.foo.bar" via a redirect response, but there you are really getting complicated).
So I will rectify A above as follows :

>   A. the href will be "http://dodo.foo.com/someotherpage.html";

The easiest way to do that, would be for all your pages to use only links without a hostname, like :
<a href="/someotherpage.html">go there</a>

If the broser got such a page via an original URL like "http://dodo.foo.com";, and he gets a page that contains a link to "/someotherpage.html", then the browser will continue to address the same server to get that page, like :
"http://dodo.foo.com/someotherpage.html";

And that will work, because of the Proxy rules above (he will get the content of the file "http://foo.com/bar/someotherpage.html"; )

-------------

So now let's take the situation where "foo.com" and "dodo.foo.com" are really the same machine and the same Apache.
That is in fact easier.

First, you would have to define (in the DNS system), that "foo.com" and "dodo.foo.com" resolve to the same IP address, the one of your server.
That is the first indispensable step.

Then, you would make it so that your Apache server knows to answer to both these names. That, you do simply with :

ServerName dodo.foo.com
ServerAlias foo.com

So now, whether the client addresses "http://dodo.foo.com"; or "http://foo.com";, he really comes to the same place.

Let's also say that the DocumentRoot is
DocumentRoot /var/www/foo.com/

and that there is a document called "index.html" there.

So when the client addresses "http://dodo.foo.com"; OR "http://foo.com";, he always gets the file /var/www/foo.com/index.html.

That is getting closer, but still not entirely what you want.
You want that when he asks for "http://dodo.foo.com";, he would actually get the document that is located at

/var/www/foo.com/bar/index.html

For that, we need mod_rewrite and a a RewriteRule

RewriteRule "^/(.*)$" "/bar/$1"

So now when the client addresses "http://dodo.foo.com"; OR "http://foo.com";, he always gets the file /var/www/foo.com/bar/index.html. (In fact, no matter what URL he asks, he will always get a "/bar/" added in front.)

That's good, but still not correct maybe, because maybe you want that this only happens when the client asks for "http://dodo.foo.com";, and NOT when he asks for "http://foo.com";.
No problem, we can put a condition on the above Rewrite :

RewriteCond %{HTTP_HOST} "dodo.foo.com"
RewriteRule "^/(.*)$" "/bar/$1"

So now we *only* do this when he asks for "dodo.foo.com" and NOT when he asks for another server name (like "foo.com").

Ok for the idea ?
You can do a lot more of this stuff. You just have to be careful not to get in a loop.


Now, go read the docs for all those directives.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx
  "   from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx


[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux