Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created February 9, 2012 14:00
Show Gist options
  • Save felipelavinz/1780150 to your computer and use it in GitHub Desktop.
Save felipelavinz/1780150 to your computer and use it in GitHub Desktop.
Canonical redirects for Apache, lighttpd and nginx
## Canonical redirect for Apache
# BEGIN Canonical Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] ## will match any domain that's not our main domain
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
</IfModule>
# END Canonical Redirect
## Canonical redirect for lighttpd
$HTTP["host"] == "domain.com" {
url.redirect = (
"^/(.*)" => "http://www.domain.com/$1"
)
}
# for various domains
$HTTP["host"] =~ "(domain.com|domain1.com|domain2.com)" {
url.redirect = (
"^/(.*)" => "http://www.domain.com/$1"
)
}
## Canonical redirect for ngingx
# as separate vhost rules
server {
listen 80;
server_name domain.com; # add other domains separated by a space as necessary
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}
# as a condition on an existent vhost
server {
listen 80;
server_name www.domain.com domain.com domain2.com www.domain2.com
if ( $host != 'www.domain.com' ) {
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}
# ... other vhost configs
}
@SadnanRashid
Copy link

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment