Forum

A question on redirecting

James
12 December 2013, 16:52
I'm running a site, and I want it to always be www.whatever.com.

I'm trying to write a toolkit that will redirect requests as follows:
If the domain is www.whatever.com, leave it alone
If the domail is whatever.com, redirect to www.whatever.com with the same path and (preferably) the same protocol.
e.g.,
http://whatever.com/ > http://www.whatever.com/
http://whatever.com/index.json > http://www.whatever.com/index.json
(this would be nice:)
https://www.whatever.com/ > https://www.whatever.com/

Hiawatha version: 9.3
Operating System: Arch Linux
Hugo Leisink
12 December 2013, 19:21
Rewriting the hostname can't be done with the UrlToolkit. But you can use this script to redirect: https://github.com/DavidOliver/www_subdomain_redirect/blob/master/index.php
Create a virtualhost for the non-www hostname and point it to the supplied script.
James
14 December 2013, 20:20
Absolutely perfect!

I set up a VirtualHost:
VirtualHost {
Hostname = domain1.com, domain2.com
WebsiteRoot = /srv/http/hiawatha/wwwRedirect
StartFile = index.php
ExecuteCGI = yes
}

/srv/hiawatha/wwwRedirect/index.php is that file with the www to non-www code commented out. It owrked like a charm!
This topic has been closed.