Forum Moderators: phranque

Message Too Old, No Replies

I need to redirect specific requests to a different server.

mod_rewrite, redirect

         

heygrady

7:19 am on Apr 27, 2004 (gmt 0)

10+ Year Member



I have a server setup that is limited to 1 external IP address. However, I have 2 servers. ServerA handles mail and webmail, ServerB handles webtraffic and FTP.

Since I can only point port 80 to 1 server, ServerB needs to redirect traffic for webmail to port 82 which goes to ServerA.

I want to have the system set up so that a request for [mail.website.com...] gets redirected to [mail.website.com:82...]

As simple as that sounds, I cannot seem to get either mod_rewrite or the Redirect directive to redirect based on the subdomain requested. I have had no trouble at all using Redirect for redirecting a call for anysubdomain.website.com/mail to mail.website.com:82

How do you redirect based on the subdomain or even the domain requested?

jdMorgan

2:34 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



heygrady,

Welcome to WebmasterWorld [webmasterworld.com]!

You can use


RewriteCond %{HTTP_HOST} ^whatever\.example\.com

followed by your RewriteRule to selectively redirect based on the subdomain.

Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular expressions tutorial [etext.lib.virginia.edu]
examples on WebmasterWorld [google.com]

Jim

heygrady

7:37 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



Thanks, that was a great hint. I had thought last night in a dream the RewriteCond might be the key. This is the Rewrite that I used. It checks to make sure that if someone looks for webmail, email or mail as a subdomain then they will be forwarded to mail.website.com:82.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www)?\.?(webmail¦email¦mail)\.(.*?)\.(.{3})$
RewriteRule ^.*?$ [mail.%3.%4:82...] [R]

I am having a problem that this is slightly slowing down my webserver and I suspect it is because EVERYTHING gets caught by the rule so the condition is always checked. Is this the best way to do it?

[edited by: heygrady at 8:13 pm (utc) on April 27, 2004]

jdMorgan

7:46 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can speed it up a little, but yes, it is checking each request. If you know that certain 'items' don't need to be redirected, then exclude them.

I removed two instances of the construct ".*?" -- I don't know what was intended, but it's not needed. In the first case, I replaced it with a faster forward-looking negative compare ([^.]*), and in the second with ".*"


RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www)?\.?(webmail¦e?mail)\.([^.]*)\.(.{3})$
RewriteRule .* http://mail.%3.%4:82/ [R=302,L]

There is probably a better way to do this, so you might just consider mod_rewrite as a band-aid until you find the right way to configure this.

Jim

heygrady

8:11 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



.*? is generally used to make non-greedy expressions. it basically tells the * to quit gobbling up characters at the soonest possible character. I have always used non-greedy *? instead of * to make sure that I don't gobble up more than I intended.

I will try out your expression, thanks again for the superb help.

Also, I had thought about having the function only work if there was nothing after the url (like only accepting mail.website.com and not accetpting mail.website.com/blah/blah/blah) but upon reflection, that would effectively make the fix much less transparent to the user. As it stands, it might be slightly slower but it won't ever throw a page not found error in their face when they aren't expecting it.

jdMorgan

8:26 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Missed a couple more optimizations:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(ww[b]w\.)?(web¦e?)mail\[/b].([^.]*)\.(.{3})$
RewriteRule .* http://mail.%3.%4:82/ [R=302,L]

Re: non-greedy. I'm not sure that works in mod_rewrite. If you have a good test case, please let me know!

Jim

heygrady

8:46 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



well, it wasn't NOT working. So that is a plus. I am afraind I am not knowledgable in either regular expressions or mod_rewrite enough to offer a good test. The reason i figured it would work is that it claims to accept perl compatible regular expressions. And non-greedy is part of the PCRE spec.

A test might be trying to match "www.1.2.3.4.5.com" with the PCRE "(www\.)(.*\.)(com)". It is concievable that the greedy .* would gobble "1.2.3.4.5" to make the match but I can't promise that because I haven't tried it. Just a thought.