linux - 使用虚拟主机从非 www 域名重定向到 www 不起作用

标签 linux apache redirect virtualhost linode

我在重定向我的 http://domain.co.uk 时遇到问题至 http://www.domain.co.uk 。我对使用 apache 和 linux 还很陌生,所以这就是我到目前为止所做的。希望有一个明显的错误可以很容易地修复。

在我尝试使重定向工作时,我发现一些教程说我应该通过虚拟主机文件来完成此操作,因此我编辑了最初为站点设置的现有虚拟主机文件

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

我已在该文件的顶部添加了一个新 block ,因此它现在看起来像这样:

<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>

编辑此文件后,我重新启动了 Apache 并尝试了该站点,但 http://domain.co.uk给我“无法打开页面 - 无法到达服务器”。 http://www.domain.co.uk仍然工作正常。

这里适合吗 <virtualhost>堵塞?我看到有人提到将虚拟主机信息放在 httpd.conf 中文件,但我在安装的 apache 中找不到该文件。

任何帮助都会很棒。

最佳答案

尝试这个并重新启动 Apache

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

关于linux - 使用虚拟主机从非 www 域名重定向到 www 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20695295/

相关文章:

c - 在 Linux C 控制台应用程序中模拟按键

java - 无法访问新的 OFBiz 16.11 插件

apache - 在同一个 Apache 中将 https 重定向到 https

JavaScript 页面重定向

php - 在 PHP 中创建一个网络服务器(没有 Apache)

php - 302 图像在浏览器中重定向速度较慢

python - 在 Linux 上将 SQL Server Native Client 与 Pyodbc 结合使用

c++ - 在 C++ 项目中包含库有哪些不同方法?

python - 当另一个程序正在使用串行端口时,我如何连接并向串行端口发送命令?

apache - htaccess文件中继承的RewriteOptions有什么作用?