Wednesday, May 6, 2009

Convert url from non-www version to www version or vise versa


Redirect with Apache
Redirect www to non-www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]


An alternative which includes domain name is as follow:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

The following is the version to redirect non-www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

WWW Redirect with nginx

Redirect to non-www, add the following code:

server {
listen 80;
server_name www.example.com;
rewrite ^/(.*) http://example.com/$1 permanent;
}

The word permanent is key. It turns the redirect into 301 redirection. After this block, you may configure the domain without www.

Redirecting non-www to www:

server {
listen 80;
server_name example.com;
rewrite ^/(.*) http://www.example.com/$1 permanent;
}

WWW Redirect with lighttpd

Redirecting www to non-www can be done with the following code:

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

Redirecting non-www to www:

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

No comments:

Post a Comment

Subscribe