Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to 301 redirect all example.org to www.example.org. The following example is doing just that, but it's got a lot of noise to it and is thus hard to maintain and error prone:

<VirtualHost 192.0.2.123:80>
        ServerName      example.org
        RedirectPermanent       /               http://www.example.org
</VirtualHost>
<VirtualHost 192.0.2.123:80>
        ServerName      www.example.org
        DocumentRoot    /usr/local/www/example
</VirtualHost>

Do you happen to know if there's some shorter version for the whole thing above?

Something like this pseudo-config:

<VirtualHost 192.0.2.123:80>
        ServerName      www.example.org
        ServerAlias     example.org
#       Redirect-Every-ServerAlias-To-ServerName
        DocumentRoot    /usr/local/www/example
</VirtualHost>

So that way I would just need to provide every subdomain that should be redirected under ServerAlias ?

share|improve this question
    
You can use mod_rewrite to redirect all requests to example.org to a www.example.org -- only 2 lines (well, 3 including engine activation directive). –  LazyOne Aug 16 '11 at 9:03
add comment

2 Answers

Use mod_rewrite, something like this:

RewriteEngine On
RewriteCond %{HTTP_HOST}  !^www.example.org [nocase]
RewriteRule ^(.*)$        http://www.example.org$1 [last,redirect=301]
share|improve this answer
1  
A small improvement to this is to use !^www.example.org in your RewriteCond which will make it continue working no matter how many ServerAlias directives your vhost has. –  Ladadadada Aug 16 '11 at 9:50
    
Thanks for the update, womble. –  O G Aug 16 '11 at 11:06
add comment
<VirtualHost *:80>
    ServerName www.example.org
    DocumentRoot /usr/local/www/example
</VirtualHost>

<VirtualHost *:80>
    ServerName example.org
    ServerAlias *.example.org another.org *.another.org
    Redirect 301 / http://www.example.org/
</VirtualHost>

make sure to put them in the right order, www first

share|improve this answer
1  
Your answer is essentially the same as the example he gave of what he is currently doing. –  Ladadadada Nov 16 '11 at 13:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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