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 good structure for search engines (SEO) or to generate accurate stats.

Configure no www

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

Find out your virtual domain section.

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" ) }

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

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 4 comments… read them below or add one }

1 chadrick 08.05.06 at 11:30 am

Nice!

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

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

Cheers,

Chadrick

2 Hendry Lee 10.17.08 at 10:46 am

Redirecting using server-level solution is always better than if done with plugin.

I have written a complete tutorial on how to do domain redirection in Apache, nginx and lighttpd.

http://blogbuildingu.com/articles/www-redirect-right-way

3 peter 01.13.09 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]

4 Geoff 01.19.09 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!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

Previous post: Understanding Linux initrd – initial RAM disk

Next post: Debian Linux Adopts OpenVZ Virtualization Software