Forum Moderators: phranque
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?
Welcome to WebmasterWorld [webmasterworld.com]!
You can use
RewriteCond %{HTTP_HOST} ^whatever\.example\.com
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
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]
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]
Jim
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.
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.