开发者

Need help configuring 301 permanent redirect in Apache for non www

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first v开发者_Python百科ariable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks


Add the following:

# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST}   !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST}   !=""
RewriteRule ^/(.*)         http://www.boom.com/$1 [L,R=301]

This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).


If you have named virtual hosts you could put the extra RewriteCond entries @tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:

<VirtualHost boom.com:80>
    RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>

I'm sure someone will comment if there's a reason to use one over the other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜