When user is visiting by www.domain.name
, redirect to domain.name
.
views:
59answers:
3
+2
A:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.name$ [NC]
RewriteRule (.*) http://domain.name/$1 [R=301,QSA,L]
jspcal
2010-01-12 04:29:26
That requires mod_rewrite. Just using Redirect (or RedirectMatch) can be done using only the more commonly enabled mod_alias.
Ry4an
2010-01-12 04:32:01
no you can't use Redirect for that
jspcal
2010-01-12 04:38:45
A:
In Apache you add a Redirect line to your configuration files. In php you reply with a status code 301 and a Location: header.
However, a redirect requires an additional network round trip. Are you sure you don't want to just use a ServerAlias line so that the same content is served up whether they visit with or without the www.?
Ry4an
2010-01-12 04:30:54
+1
A:
Put this in an .htaccess
file in your root directory of your website:
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.name
RewriteRule (.*) http://domain.name/$1 [R=301,L,QSA]
This is what they do, in order:
- Turn the rewrite engine on
- Make sure that
HTTP_HOST
was provided - If it doesn't start with the name without the
www
or any other sub domain, then allow the rewrite to continue. This prevents an endless redirect back to itself. - Grab everything after the URL
(.*)
, including the querystringQSA
, and redirectR=301
to the correct domain. TheL
just says this is the last command in the file if a match is found.
Doug Neiner
2010-01-12 04:31:24
No, I believe it allows you to access the site via IP address without being redirected.
Doug Neiner
2010-01-12 04:39:37