dongren4099 2014-01-31 06:52
浏览 61
已采纳

Apache VirtualHost无法正常工作

Problems

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>

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:

/Apps/XAMPP/htdocs (The real Homepage) is what http://wrks.tk loads

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


Attempts of Solution

  • Emptying cache
  • Restarting Apache multiple times
  • Checking the error log (Didn't have any errors that helped)

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.

But to explain it better,

Connect to http://google.com/ and notice how it redirects you to http://www.google.com/

What I want is basically the opposite, but the VirtualHosts aren't letting me do that.

  • 写回答

1条回答 默认 最新

  • dongzhong8834 2014-01-31 07:06
    关注

    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>
    

    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]
    

    Remove completly the REDIR dir with the contents. This will htaccess will redirect website which starts with www.to a non-www.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64