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.

Using virtual hosts I have gotten into the habit of using this:

<VirtualHost: *:80>
    ServerName www.example.com
    Redirect permanent / http://example.com/
</VirtualHost>

Yet I often see rewriting used instead:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Is there any advantage to one approach over the other?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

If you don't need mod_rewrite then don't use it. For simple cases like this mod_alias is perfectly sufficient, and much simpler to understand. Also, while minor, mod_alias is faster than mod_rewrite -- this can make a difference on high volume sites.

share|improve this answer
add comment

Those two directive result in the same 301 code being sent back to the browser. So no advantage to one or the other. You should choose one based on whichever you are more comfortable using and makes reading the configuration file easier.

Both are capable of also sending something other than the 301 (Permanent Redirect) code. You should always use a 301 when eliminating (or requiring) the "www" for clarity to SEO and browsers. The other types of redirects are used less commonly (very infrequently); a "temporary redirect" is the most common of them, and should only be used when the destination of the redirect will change often (for example, a link your most recent blog entry or something like that).

share|improve this answer
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.