Forum Moderators: phranque

Message Too Old, No Replies

Hiding redirected URL using mod rewrite.

         

mnsweeps

1:37 am on Dec 20, 2010 (gmt 0)

10+ Year Member



I have a apache web server running on my HP EX485 at port 80. There are other servers running on other ports like 8080, 8090 , 8089... I want to redirect from apache based on the directory requested to one of these services..

[mysite.com...] redirect to [mysite.com:8080...]
[mysite.com...] redirect to [mysite.com:8089...]
[mysite.com...] redirect to [mysite.com:8090...]

example for /games, I have the rewriterule setup as follows in the .htacces file in /games directory.

# now the rewriting rules
RewriteRule ^(.*)$ [%{HTTP_HOST}:8090...] [R=301]

The rewrite happens in browser correctly but the browser url changes to
[mysite.com:8090...] and I do not want that )

any help would be appreciated. Thanks

g1smd

9:13 am on Dec 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A redirect tells the browser to close the current HTTP transaction and then request a new URL in a new HTTP transaction. That's what the HTTP '30x' status code signifies.

You'll need a Proxy passthrough instead, but this results in your server having to do twice as much work to service each request.

mnsweeps

4:21 pm on Dec 20, 2010 (gmt 0)

10+ Year Member



Thanks G1smd. I was under the impression that the rewrite in mod_rewrite is a misnomer and can be used for external redirect or internal rewrite. Please refer to jdmorgan's reply at [webmasterworld.com...]

g1smd

7:35 pm on Dec 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It can be used for internal rewrites, but if you include a protocol and domain or the R flag (or both) you will get a 302 external redirect.

If you need a 301 redirect you need the R=301 flag.

If you need an internal rewrite you must omit the protocol, domain and R flag. None of those must appear. You DO need the [L] flag on every rule.

mnsweeps

7:39 pm on Dec 20, 2010 (gmt 0)

10+ Year Member



So are you suggesting I use a IP address like this ?

RewriteRule ^(.*)$ [11.12.13.14:8090] ( assuming 11.12.13.14 is IP of my server)

g1smd

7:44 pm on Dec 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That rewrite rule will look for a file called 11.12.13.14:8090 inside your server in order to try to service every request arriving at your server.

mnsweeps

7:53 pm on Dec 20, 2010 (gmt 0)

10+ Year Member



okay how about

RewriteRule ^(.*)$ [http://11.12.13.14:8090] ( assuming 11.12.13.14 is IP of my server) but then I am using a protocol here :)

g1smd

8:08 pm on Dec 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That, as explained above, will generate an external 302 redirect.

jdMorgan

12:42 am on Dec 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot specify a protocol and hostname or an IP address. Doing so creates a redirect as explained above.

You will need to use the [P] flag to invoke a reverse proxy to the 8090 port... and override the above-described redirect behaviour.

See mod_rewrite [P] flag and Apache mod_proxy docs for details on reverse-proxy implementation.

You will probably also need to take steps to set up proper request logging on the proxied-to server: Configure mod_proxy on the front-end server to send the X-Forwarded-For header to the back-end server, and configure the back-end server to log X-Forwarded-For instead of Remote-Host or Remote-Address.

Jim