Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

javascript - Apache VirtualHost not working correctly

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...