Redirect www to non-www Preserving HTTP(S) for Lighttpd
First new blog post in a while… I really haven’t been able to find the time. But university finishes soon so hopefully I’ll have the time to while away some hours on here!
I thought this was worth blogging about as I was Googling around for ages without finding a full answer to this problem anywhere. Namely, I found posts about redirecting HTTP to HTTPS and www to non-www, but none about both. Hopefully this will help and make it easy for others.
I assume you already have HTTPS support up and running on your server, though if you don’t, see this post on the Lighttpd website for a simple how-to.
Edit your lighttpd.conf file to include the following:
The first section checks if you’re using HTTPS. If you are, it checks if the URL has “www.” at the front. If it does, then it strips it but keeps the https in the URL! It does exactly the same thing for HTTP, except it preserves the http in the URL.
To put it into action, simply restart the Lighttpd server using something like
sudo /etc/init.d/lighttpd restart