如何使用Apache2将我的网站从非wwwredirect到WWW?

我目前正在设置我的个人网页。

我使用的是VPS,并且手动安装了Wordpress,而且一切似乎都奏效了。除非我去了我的网站的非www版本,否则它会出现一个未find的页面。

www.andrewrockefeller.com < – 工作

andrewrockefeller.com < – 不(我想把它redirect到www.andrewrockefeller.com)

我已经尝试添加RewriteEnginefunction到我的.htaccess,这是行不通的。

我也尝试添加添加到我的默认文件(这是apache2.conf从以下拉:

<VirtualHost *> ServerName andrewrockefeller.com Redirect 301 / http://www.andrewrockefeller.com/ </VirtualHost> 

看到有多less人能够得到上述的工作,是否有什么我可能会错过,让这个function?

感谢您的时间!

编辑:我的.htaccess文件如下所示:

 # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 

当我将?p = 1(丑陋链接)的设置更改为漂亮链接时,#Wordpress部分被自动创build。 任何build议的解决scheme,我已经在这里find我已经尝试并重新启动apache2,它没有奏效。

问题1:不正确的DNS

您没有www.andrewrockefeller.com的A或CNAMElogging。 因此,浏览器无法将您的域名转换为IP地址。

 dig andrewrockefeller.com ; <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5_8.2 <<>> andrewrockefeller.com ;; global options: printcmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31887 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0 ;; QUESTION SECTION: ;andrewrockefeller.com. IN A ;; AUTHORITY SECTION: andrewrockefeller.com. 1739 IN SOA dns226.a.register.com. root.register.com. 2013082003 28800 7200 604800 14400 ;; Query time: 22 msec ;; SERVER: 8.8.8.8#53(8.8.8.8) ;; WHEN: Thu Oct 31 12:44:47 2013 ;; MSG SIZE rcvd: 98 

出现您的域名在Register.com注册。 在他们的门户上,你需要添加一个Alogging

 andrewrockefeller.com -> 76.164.206.80 

一旦你添加这个Alogging,你的domain.com版本应该解决。

问题2:规范域名

我认为Wordpress会正确地redirect域名,但为了保证这一点,你最好强迫任何不符合你想成为你想要的东西。

参考: http : //wiki.apache.org/httpd/CanonicalHostNames

我更喜欢使用mod_rewrite和这个方法:

 # And for a site running on port 80 RewriteCond %{HTTP_HOST} !^example\.com [NC] RewriteCond %{HTTP_HOST} !="" RewriteRule ^/?(.*) http://example.com/$1 [L,R=301] 

这基本上说,如果域不匹配example.comredirect到example.com。 如果您想在www.example.com上实现标准化,则可以相应地进行修改:

 # And for a site running on port 80 RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] RewriteCond %{HTTP_HOST} !="" RewriteRule ^/?(.*) http://www\.example.com/$1 [L,R=301] 

你的答案在于DNS。 您的域名注册商register.com应该提供一个选项来pipe理您的DNS条目。 在您的控制面板的这一部分,可能会有一个选项,包括www。 子域。

如果没有,你可以自己创build它作为一个子域,指向你的主机的根文件夹。

请参阅: http : //help.register.com/app/answers/detail/a_id/2882

 <If "%{HTTP_HOST} != 'www.example.com'"> Redirect / http://www.example.com/ </If> 

正如在Canonical Hostnames Apache Docs Remapping / Rewriting下解释的那样