Skip to content

Instantly share code, notes, and snippets.

@tispratik
Forked from felipelavinz/apache
Created January 8, 2014 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tispratik/8312636 to your computer and use it in GitHub Desktop.
Save tispratik/8312636 to your computer and use it in GitHub Desktop.
## 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment