To do the rewriting I'll be focusing on Apache's mod_rewrite, which is one of the more widely available solutions to the problem.
This code is what you need if you use a .htaccess file on your server:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.chosendomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.chosendomain.com/$1 [R=301,L]
There's another option when you want to keep it stored in a safer location, which is including it in Apache's httpd.conf file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.chosendomain\.com$ [NC]
RewriteRule ^/(.*)$ http://www.chosendomain.com/$1 [R=301,L]
These two blocks of code have slight differences. Notice RewriteRule here has a forward slash, replacing the RewriteBase / directive on the previous example. While they do the same thing, they are not interchangeable depending on which configuration file you use.
Don't forget the escape character for the dot on RewriteCond rules - even though it is unlikely you'll match something.
If after you've done this, you need to exclude some folders/paths from the URL redirection you've just inserted, see the instructions on the Selectively Disabling mod_rewrite post.
No comments:
Post a Comment