Lighttpd Redirect Requests To Or Vice Versa

by on August 4, 2006 · 7 comments· LAST UPDATED July 17, 2009

in , ,

Lighttpd logo

For various reason you might want to redirect all traffic coming to to or vise versa.

This is essential if you want to maintained a good structure for search engines (SEO) or to generate accurate stats.

How To Configure No www

Open your /etc/lighttpd/lighttpd.conf file:
# vi /etc/lighttpd/lighttpd.conf

Find out your virtual domain section.

WWW Redirect with lighttpd

Add following configuration directive if you want to redirect to (no www)

$HTTP["host"] =~ "^www\.(.*)"
			url.redirect = ( "^/(.*)" => "http://%1/$1" )

No-WWW Redirect with lighttpd

Add following configuration directive if you want to redirect to (force www)

 $HTTP["host"] =~ "^domain\.com$"
			url.redirect = ( "^/(.*)" => "$1" )

Make sure you replace with your actual domain name. You can see live example of this configuration by visiting this url (it will automatically remove www and redirect you to domain).

Close and save the file. Restart the lighttpd web server:
# /etc/init.d/lighttpd restart

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 7 comments… read them below or add one }

1 chadrick August 5, 2006 at 11:30 am


But how do I redirect all feed traffic to a feedburner account?

If someone make visit to =>




2 peter January 13, 2009 at 3:18 am

How can i redirect user based on referer?
Sometime i got bad user i want to redirect them to goway.html? How can i do that? In apache i do something like this:

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} ^http://(.+)\.edublogs\.org [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?fakeyahoo\.com [NC,OR]
RewriteRule ^(.*)$ http://mysitehere/getout.html [R=301,L]


3 Geoff January 19, 2009 at 11:29 pm

Does anyone know how I can redirect all non-ww domains to the www version?

I basically need this reversed:

$HTTP[“host”] =~ “^www\.(.*)$” {
url.redirect = ( “^/(.*)” => “http://%1/$1″ )

Thanks for any help!


4 Jush Fuskin July 17, 2009 at 3:54 pm

I added the directive into my .conf file and it worked but the problem is when I visit a url

for example: when I go to it sends me to Which is what I want.

But when I go to Instead of adding a www. infront of the title it just redirects me to the instead of

How do I fix this?


5 nixCraft July 17, 2009 at 4:37 pm

Did you add $1 at the end of domain name?


6 Jush Fuskin July 17, 2009 at 4:54 pm

Yes I have, here is the exact code I am using.

$HTTP[“host”] =~ “(^|\.)domain\.com$” {
url.redirect = ( “^/(.*)” => “$1″ )
server.document-root = “/var/www/html/”
server.error-handler-404 = “/404.php”


7 Franz December 22, 2010 at 9:40 am

How can I enable this for HTTPS, too? This rewrite rule seems to redirect to HTTP automatically.


Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: