Apache 使用mod_重写添加“www”

Apache 使用mod_重写添加“www”,apache,mod-rewrite,web,no-www,Apache,Mod Rewrite,Web,No Www,可能重复: 我有一个网站,可以使用www和不使用www访问,例如www.example.com和example.com。如果URL中没有www,则使用mod_rewrite重定向到www.example.com 但是,如果我转到example.com/something.html,它会被重定向到www.example.com something.html,它会被截断 目前,我的规则是: <VirtualHost *:80> ServerName example.com

可能重复:

我有一个网站,可以使用www和不使用www访问,例如www.example.com和example.com。如果URL中没有www,则使用mod_rewrite重定向到www.example.com

但是,如果我转到example.com/something.html,它会被重定向到www.example.com something.html,它会被截断

目前,我的规则是:

<VirtualHost *:80>
    ServerName example.com
    RewriteEngine On
    RewriteRule /.* http://www.example.com [R]
</VirtualHost>

使重写递归并使example.com/something.html重定向到www.example.com/something.html的最简单方法是什么?

很难相信以前没有人问过这个问题。。。。。提示:搜索mod_重写prepend www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]