Forum Moderators: phranque
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I have also tried this one:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
and I've tried this one:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www.example.com/ [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
The first and third redirect requests for any domain or subdomain NOT matching the canonical domain, while the second is intended to redirect a specific non-canonical domain to the canonical domain. The second snippet has a fatal flaw, however, in that hostnames don't contain a "/", so it won't work.
If none work, then I'd expect you to be seeing errors in your server error log; Anything there?
Here is the code, cleaned-up and documented:
# For use in document-root .htaccess
# Enable mod_rewrite (Required on some but not all server configurations; May cause an error if not needed)
Options +FollowSymLinks
# Turn on the rewriting engine
RewriteEngine on
# If request Host header is non-blank (HTTP/1.0 requests don't send this header and can't be redirected based on it)
RewriteCond %{HTTP_HOST} .
# And if requested domain is NOT the canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com
# Redirect to requested page in canonical domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# For use in document-root .htaccess
# Enable mod_rewrite (Required on some but not all server configurations; May cause an error if not needed)
Options +FollowSymLinks
# Turn on the rewriting engine
RewriteEngine on
# If non-canonical domain requested (case-insensitive compare)
RewriteCond %{HTTP_HOST} ^example\.com [NC]
# Redirect to requested page in canonical domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# For use in httpd.conf or conf.d
# Enable mod_rewrite (Required on some but not all server configurations; May cause an error if not needed)
Options +FollowSymLinks
# Turn on the rewriting engine
RewriteEngine on
# If requested domain is NOT the canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com
# And if request Host header is NOT blank (HTTP/1.0 requests don't send this header and can't be redirected based on it)
RewriteCond %{HTTP_HOST} !^$
# Redirect to same page in canonical domain
RewriteRule ^/(.*) http://www.example.com/$1 [R=301,L]
If none of these work, then I'd suspect that you have a server configuration problem, or that mod_rewrite is not available to you on that host.
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
The second snippet has a fatal flaw, however, in that hostnames don't contain a "/", so it won't work.
Sorry. That was just a typo... but thanks for the heads-up on that.
No, I've not had any server errors by trying those codes. I've just not felt that they were entirely correct since the results weren't as expected. Mod-rewrite is definitely available. I'll give your first version a try.