Rewrite rule: webmail.domain.com to domain.com/webmail

Hi,

I want to write a simple serverwide rewrite rule in server so that

http://webmail.domain.com

is redirected to

http://domain.com/webmail

I am using ISPConfig on centos

Thanks

Richard

13 Replies

````
ServerName webmail.example.com RedirectMatch /(.*)$ http://example.com/webmail/$1

````

In this case .. I will have to write this for all domains .. I am hosting more then 250 domains…

can there be a generic rule ..?

Rewrite webmail.(.*) $1/webmail

No generic rule possible AFAIK.

There is the ServerAlias directive which allows for associating multiple names (and can have wildcards) with a single VirtualHost section.

http://httpd.apache.org/docs/2.2/mod/co … erveralias">http://httpd.apache.org/docs/2.2/mod/core.html#serveralias

While it does allow for wildcards, I have personally only ever used a leading wildcard (eg *.example.com), but I'd have a look at that at least to see if it can solve your issue together with the appropriate rewrite rules.

I think something like this may work:

RewriteCond %{HTTP_HOST} ^webmail\.(.+) [NC]
RewriteRule (.*) http://%1/webmail/$1 [R=301,L]

I tried all that … but just not working

@richardvc:

I tried all that … but just not working

Can you elaborate a bit on how it doesn't work?

What exactly have you done and what happens?

I ran a quick test and this seems to work fine:

 <virtualhost *:80="">ServerAlias webmail.*

        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^webmail\.(.+) [NC]
        RewriteRule (.*) http://%1/webmail$1 [R=301,L]</virtualhost> 

Of course, with the rewrite rules there you do not technically need a specific VirtualHost for this task, but I think it's kind of nice in case you ever want any other configuration specific to these hosts.

(As for the removed slash in the rewriterule compared to what I originally suggested, it's to avoid getting double slashes in the destination url, it didn't really break anything but looked bad.)

The redirection works …

But we get a blank page.

e.g.

http://webmail.dsvtrust.org/ redirects to http://dsvtrust.org/webmail/

If I access the URL in new page, http://dsvtrust.org/webmail/, it works nicely

It is going in infinite loop …

rewrite log …

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial init rewrite engine with requested uri /

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial applying pattern '(.*)' to uri '/'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial rewrite '/' -> 'http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial explicitly forcing redirect with http://cjrumao.com/webmail//

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial applying pattern '(.*)' to uri 'http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial rewrite 'http://cjrumao.com/webmail//' -> 'http://cjrumao.com/webmail/http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial explicitly forcing redirect with http://cjrumao.com/webmail/http://cjrumao.com/webmail//

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial applying pattern '(.*)' to uri 'http://cjrumao.com/webmail/http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial rewrite 'http://cjrumao.com/webmail/http://cjrumao.com/webmail//' -> 'http://cjrumao.com/webmail/http://cjrum … /webmail//">http://cjrumao.com/webmail/http://cjrumao.com/webmail/http://cjrumao.com/webmail//'

1

This setting finally worked ..

ServerAlias webmail.*

RewriteEngine On

RewriteLog "/var/log/httpd/rewrite.log"

RewriteLogLevel 3

RewriteCond %{HTTP_HOST} ^webmail.(.+) [NC]

RewriteRule (.*) http://%1/webmail/ [R=301,L]

@richardvc:

It is going in infinite loop …

rewrite log …

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial init rewrite engine with requested uri /

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial applying pattern '(.*)' to uri '/'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial rewrite '/' -> 'http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial explicitly forcing redirect with http://cjrumao.com/webmail//

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial applying pattern '(.*)' to uri 'http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial rewrite 'http://cjrumao.com/webmail//' -> 'http://cjrumao.com/webmail/http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial explicitly forcing redirect with http://cjrumao.com/webmail/http://cjrumao.com/webmail//

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial applying pattern '(.*)' to uri 'http://cjrumao.com/webmail/http://cjrumao.com/webmail//'

120.60.138.194 - - [13/Jan/2011:09:15:10 +051800] [webmail.cjrumao.com/sid#b8522230]rid#b883d510/initial rewrite 'http://cjrumao.com/webmail/http://cjrumao.com/webmail//' -> 'http://cjrumao.com/webmail/http://cjrum … /webmail//">http://cjrumao.com/webmail/http://cjrumao.com/webmail/http://cjrumao.com/webmail//'

1

Without knowing what the configuration looked like when this happened it's very hard to point out the problem (but it's clear that it was not http://forum.linode.com/viewtopic.php?t=6538#35400, right?)

Glad to hear it works, anyway! (But strips any path that was specified? Maybe that's ok?)

Yes, I am okay with removing remaining part of the url ..

My clients will type http://webmail.domain.com

Thank you for your help .. this type of setup is requested many times on ISPConfig forum .. but no body was able to handle it because the way ISPConfig works … I will prepare a simple FAQ there ..

Richard

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct