≡ Menu

Lighttpd Redirect www.domain.com Requests To domain.com Or Vice Versa

Lighttpd logo

For various reason you might want to redirect all traffic coming to www.domain.com to domain.com 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 www.domain.com to domain.com (no www)

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

No-WWW Redirect with lighttpd

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

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

Make sure you replace domain.com 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 theos.in domain).

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

Share this on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 8 comments… add one }
  • chadrick August 5, 2006, 11:30 am


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

    If someone make visit to domain.com/feed/ => feeds.feedburner.com/myAccount



  • peter January 13, 2009, 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]

  • Geoff January 19, 2009, 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!

  • Jush Fuskin July 17, 2009, 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 http://mydomain.com it sends me to http://www.mydomain.com. Which is what I want.

    But when I go to http://mydomain.com/page1.php Instead of adding a www. infront of the title it just redirects me to the http://www.mydomain.com instead of http://www.mydomain.com/page1.php

    How do I fix this?

  • nixCraft July 17, 2009, 4:37 pm

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

  • Jush Fuskin July 17, 2009, 4:54 pm

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

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

  • Franz December 22, 2010, 9:40 am

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

  • fayaz April 8, 2015, 2:17 pm

    hey how are you all …
    i have two server which is running at the same time ..and having same pages like CDN network.
    for example.
    http://www.kkk.com/info.php (
    question is that…when user hit above site.they are facing 404 not found..
    i want when ever user face that error it will automatically redirect on my another server for example ( this .and get page from there and they access that automatically ..i will be very thankfull to you act of this kindness
    kindly tell me in this regards ,.

Leave a Comment

   Tagged with: , , , , , , , , , , , , , , , , , , , ,