So this is my scenario:

I have 1 static IP and 2 servers. 1 server is a web server the other is a mail server. I have a router as a hardware firewall with all the ports are set that require passthrough to internal ip addresses .

If a user types the url http://www.domain.com they see that website. If the user wants to access webmail they type in the url http://mail.domain.com but still see http://www.domain.com

I have set the webmail domain to be accessible via port 8080 on the mail server and if the user types the url http://mail.domain.com:8080 it works no problem but not with http://mail.domain.com.

So this is my issue:

In my httpd.conf I want to setup a Redirect 301 so that when the user types in http://mail.domain.com they get redirected to http://mail.domain.com:8080

I'd prefer not to use .htaccess and keep the directives in the httpd.conf



You'll need to create a virtual host on your main web server that responds to web requests for the "mail.domain.com" address. Take a look at the Name-based Virtual Host documentation on the Apache website. You'll also need a mod_rewrite rule to do the redirection. Your configuration would look something like this:

NameVirtualHost *:80
<VirtualHost *:80>
  ServerName mail.domain.com
  RewriteEngine On
  RewriteRule (.*) http://mail.domain.com:8080/\1 [R=301,L]
  • Rewrite is useless if a virtualhost is created, RedirectMatch will be probably faster – radius Jun 20 '10 at 18:22
  • what are you talking about radius? Rewrite is perfectly fine in a virtualhost as listed above. The code is a little broken (use $1 not \1) but other than that it's good. – user15590 Jun 21 '10 at 9:43

There is at least 2 way to do this:

  1. Create 2 virtual host one for www.domain.com and one for mail.domain.com and then put your RedirectMatch directive in the second one.
    You could also use the ProxyPass directive in the mail.domain.com virtual host to make it working like a reverse proxy and have mail.domaine.com:80 working directly without redirection

  2. Use mod_rewrite like this to only redirect for the mail.domain.com url with a rule like that

    RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
    RewriteRule ^(.*) http://mail.domain.com:8080/$1 [R=301]

    with mod_rewrite you can also do a reverse proxy with a rule like (mail.domain.com should resolve to the internal IP)

    RewriteCond %{HTTP_HOST} ^mail.domain.com$ [NC]
    RewriteRule ^(.*) http://mail.domain.com:8080/$1 [P]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.