Forum Moderators: phranque
I found code examples in an archived thread here, but they caused an Forbidden error when I tried them.
Here's the code I put in my top-level .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteRule ^(.*)$ [mydomain.com...] [R=301,L]
I'm getting a 403 Forbidden Error when I make this my .htaccess file - even when it's the ONLY block of code in there.
Thanks, and God bless!
-Jeff Gannaway
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*) [domain.com...] [R=permanent,L]
Note the "!" is removed as that means "not" (I think... :)
The most likely cause of your 500 Server Error is a missing space between "}" and "!" in the RewriteCond -- posting code on this board removes those spaces, and this leads to chronic errors with copied code.
The second-most-likely cause is that your server configuration will require you to add the line
Options +FollowSymLinks nancyb's code will also work just fine. The difference is that your code says, "If non-blank and not desired domain, redirect to desired domain," while nancyb's code says, "If undesired domain, redirect to desired domain."
So, nancyb's code is specific to one undesired domain, while your code will rewrite any domain except the desired one. This can be useful if you have a lot of possible domains or subdomains that resolve to your server and need to be redirected.
Jim