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