Forum Moderators: phranque

Message Too Old, No Replies

Rewrite and Proxy

How to rewrite a URL and do a proxy pass

         

carlos1234

9:22 am on Jan 31, 2010 (gmt 0)

10+ Year Member



Hello to everybody,

I have an Apache who receives the http requests and a IIS in another box with IP 192.168.0.250.

I'll like to rewrite the request and then send it to the IIS.

To do so I wrote in the httpd.conf :

<VirtualHost *:80>
ServerName www.test.com
ServerAlias test.com *.test.com

RewriteEngine On
RewriteRule ^/shoes/([A-Za-z0-9-]+)/$ /web/default.asp?shoe=$1 [L]

ProxyPass / [192.168.0.250...]
ProxyPassReverse / [192.168.0.250...]
ProxyPreserveHost On

ErrorLog logs/test_error.log
CustomLog logs/test_access.log common
</VirtualHost>

But it doesn't works, with only the proxy rules everything goes OK but with the rewrite rule nothing works.

żAny suggestion?

Thanks.

jdMorgan

12:22 pm on Jan 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the mod_rewrite RewriteRule Flags [httpd.apache.org] documentation.

Jim

carlos1234

8:07 pm on Jan 31, 2010 (gmt 0)

10+ Year Member



Thanks Jim,

I have tried also:

RewriteRule ^/shoes/([A-Za-z0-9-]+)/$ /web/default.asp?shoes=$1 [P]

But with no results as expected.

How can I see witch is the URL that receives the IIS if the page served is a 404 error?.

Where can I see an example to compare?.

Thanks again.

jdMorgan

3:45 am on Feb 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need a protocol and full URL in that rule, otherwise, this rule proxies this server to itself, and not to the back-end IIS server.

RewriteRule ^/shoes/([A-Za-z0-9-]+)/$ http://192.168.0.250/web/default.asp?shoes=$1 [P]

You don't need the ProxyPass and ProxyPassReverse directives, should not use them, and they don't look right anyway, as they appear to be proxying *all* URLs requested from this Apache server to the back-end server.

Jim

carlos1234

8:45 am on Feb 1, 2010 (gmt 0)

10+ Year Member



Hi Jim,

I have several web pages served by the back-end server with IIS, all of them are proxyed by the Apache (who serves other web pages in PHP) . In the webs served by the IIS I don't need to rewrite the URL. So, with:

<VirtualHost *:80>
ServerName www.epegatina.com
ServerAlias epegatina.com *.epegatina.com
ProxyPass / [192.168.0.250...]
ProxyPassReverse / [192.168.0.250...]
ProxyPreserveHost On
ErrorLog logs/epegatina_error.log
CustomLog logs/epegatina_access.log common
</VirtualHost>

works fine.

But now I'll like to do friendly URL's. With the web pages served by the Apache it's easy but the ones served by the IIS I'll like to do the rewrite on the Apache before the proxying.

With:

RewriteRule ^/shoes/([A-Za-z0-9-]+)/$ [192.168.0.250...] [P]

I'm not telling to the IIS witch URL to serve. Essential, I thing.

But when I tried to do your proposal (deleting the proxy lines leaving only the rewrite ones) I received the listing of the DocumentRoot of the Apache.

So, I'm confused.

I'm sure that I'm not the one who tries to do somethink like this. But I'm not able to find the correct way or an example.

Thanks Jim.

jdMorgan

2:04 pm on Feb 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, applications servers are never "easy," but you do need to know at least one important thing here, and that is that mod_proxy is usually invoked before mod_rewrite can run. Therefore, if you proxy all URLs to the back-end IIS server (as indicated by "ProxyPass /" , then no mod_rewrite rules will be executed.

That's why I said your ProxyPass and ProxyPassReverse set-up didn't look quite right.

If your front-end is Apache 2.x with PCRE support, consider using ProxyPassMatch and Perl-Compatible Regular Expressions to exclude the "/shoes" path from being directly proxied to the back-end (using a negative-pattern match).

Jim