简体   繁体   English

使用virtualhost无法从非www域名重定向到www

[英]redirect from non-www domain name to www using virtualhost not working

I'm having issues redirecting my http://domain.co.uk to http://www.domain.co.uk . 我在将http://domain.co.uk重定向到http://www.domain.co.uk时遇到问题。 I'm pretty new to working with apache and linux so this is what i have done so far. 我对使用apache和linux很陌生,所以这是我到目前为止所做的。 Hopefully there is an obvious mistake that can be fixed easily. 希望有一个显而易见的错误可以轻松解决。

In my attempt to get the redirect working I found a few tutorials saying that i should do it via the virtualhost file so i edited the existing virtual host file that i initially set up for the site in 在尝试使重定向正常工作的过程中,我发现一些教程说应该通过virtualhost文件执行此操作,因此我编辑了最初为该站点设置的现有虚拟主机文件。

/etc/apache2/sites-available/domain.co.uk /etc/apache2/sites-available/domain.co.uk

I have added a new block to the top of this file so it now looks like this: 我在该文件的顶部添加了一个新块,因此现在看起来像这样:

<VirtualHost *:80>
    ServerName domain.co.uk
    Redirect permanent / http://www.domain.co.uk/
</VirtualHost>
<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin admin@domain.co.uk
  ServerName  www.domain.co.uk
  ServerAlias domain.co.uk

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /home/simon/public/domain.co.uk/public/

  # Log file locations
  LogLevel warn
  ErrorLog  /home/simon/public/domain.co.uk/log/error.log
  CustomLog /home/simon/public/domain.co.uk/log/access.log combined
</VirtualHost>

After editing this file I restarted Apache and tried out the site but http://domain.co.uk is giving me "failed to open the page - cannot reach the server". 编辑此文件后,我重新启动了Apache并尝试了该站点,但http://domain.co.uk却给我“无法打开页面-无法访问服务器”。 http://www.domain.co.uk is still working fine. http://www.domain.co.uk仍然可以正常工作。

Is this the right place for this <virtualhost> block? 这是<virtualhost>块的正确位置吗? I have seen some mention of putting virtual host info in the httpd.conf file but I haven't been able to find this file in my installation of apache. 我已经看到有人提到将虚拟主机信息放入httpd.conf文件中,但是在安装apache时找不到该文件。

Any help would be great. 任何帮助都会很棒。

try this one and restart Apache 试试这个并重启Apache

<VirtualHost *:80>
    ServerName domain.co.uk
    ServerAlias *.domain.co.uk
    RedirectMatch permanent /(.*) http://www.domain.co.uk/$1
  </VirtualHost>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM