Apache的虚拟主机无法正常工作 [英] Apache VirtualHost not working correctly
问题描述
我遇到的问题是获得虚拟主机的工作权利。这是我有两个设置:
The problem I'm having is getting the VirtualHost to work right. This is how I have the two setup:
<VirtualHost *:80>
DocumentRoot "/Apps/XAMPP/htdocs"
ServerName wrks.tk
ErrorLog "/Logs/Workarea/Error.log"
CustomLog "/Logs/Workarea/Access.log" common
<Directory "/Apps/XAMPP/htdocs/Workspace">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Apps/XAMPP/htdocs/REDIR"
ServerName www.wrks.tk
<Directory "/Apps/XAMPP/htdocs/REDIR">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
这是什么应该,如果你连接到 http://www.wrks.tk/
你要做的就是重定向到的http:// wrks .TK /
,但出于某种原因, http://www.wrks.tk
加载完全相同的网页为 HTTP ://wrks.tk/
(而不是重定向像它应该太)。但你可以看到两个虚拟主机都指向不同的目录:
What this is supposed you do is redirect if you connect to http://www.wrks.tk/
to http://wrks.tk/
but for some reason, http://www.wrks.tk
loads the same exact homepage as http://wrks.tk/
(instead of redirecting like it's supposed too). But as you can see both virtual hosts are pointed to a different directory:
/应用/ XAMPP / htdocs中
(真正的主页)是 HTTP: //wrks.tk 负载
/Apps/XAMPP/htdocs
(The real Homepage) is what http://wrks.tk loads
/应用/ XAMPP / htdocs中/ REDIR
(重定向页)它应该重定向 http://www.wrks.tk
为 http://wrks.tk
。这是我在文件中:
/Apps/XAMPP/htdocs/REDIR
(Redirection page) it's supposed to redirect http://www.wrks.tk
to http://wrks.tk
. This is what I have in the file:
<html>
<body>
<script>window.location.replace("http://wrks.tk/");</script>
<h1 style="text-align: center;">Redirecting you to the correct link</h1>
</body>
</html>
一个简单的重定向脚本,但,而不是这样做,它只是加载网页。
A simple redirect script, but instead of doing that, it just loads the homepage.
- 清空缓存
- 重启动Apache多次
- 检查错误日志(没有帮助的任何错误)
不过,所有这些解决我的问题,这就是为什么我问这个问题了。
But, none of these solve my problem, which is why I'm asking this question now.
对于任何混乱道歉,非常相似的链接,使其混乱的理解和阅读。
Apologies for any confusion, the very-similar links make it confusing to understand and read.
但要更好地解释它,
连接到 http://google.com/ 并注意它是如何将您重定向到 http://www.google.com/
Connect to http://google.com/ and notice how it redirects you to http://www.google.com/
我想基本上是相反的,但VirtualHosts没有让我这样做。
What I want is basically the opposite, but the VirtualHosts aren't letting me do that.
推荐答案
首先你真的很接近。确保的mod_rewrite
Apache模块已启用。其次,你并不需要两个虚拟主机,这样你就可以像这样把它们合并(我离开路径不变):
Firstly you are really close. Be sure that mod_rewrite
apache module is enabled. Secondly you do not need two virtual hosts, so you can merge them like so (I left the paths untouched):
.
<VirtualHost *:80>
DocumentRoot "/Apps/XAMPP/htdocs"
ServerName wrks.tk
ServerName www.wrks.tk
ErrorLog "/Logs/Workarea/Error.log"
CustomLog "/Logs/Workarea/Access.log" common
<Directory "/Apps/XAMPP/htdocs/Workspace">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
现在在目录的htdocs
创建一个文件的.htaccess
与内容:
Now in the directory htdocs
create a file .htaccess
with content:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
与内容完全地删除了 REDIR
DIR。这将htaccess的重定向的网站,以 WWW启动。
来一个非www。
Remove completly the REDIR
dir with the contents. This will htaccess will redirect website which starts with www.
to a non-www.
这篇关于Apache的虚拟主机无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!