Page 1 of 1

.htaccess redirect to domain

Posted: Mon Jun 15, 2009 3:54 pm
by alex.barylski
I have a web site where you can access it as either 'www' or without...Ideally I would like the .htaccess code required to force a redirect to one or the other -- preferably the 'www' prefix variety.

Cheers,
Alex

Re: .htaccess redirect to domain

Posted: Mon Jun 15, 2009 5:40 pm
by pickle
This stuff is all over Google.

Re: .htaccess redirect to domain

Posted: Tue Jun 16, 2009 7:45 am
by alex.barylski
Would you mind pointing me in the direction of this 'stuff'?

Code: Select all

Force redirect to 'www'
Not sure what the hell I was searching for before but previously the results were dismal...the above popped into my head and voila I have found a few sources:

Code: Select all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^trevorfitzgerald.com [NC]
RewriteRule ^(.*)$ http://www.trevorfitzgerald.com/$1 [L,R=301]
I would like to know whether it's possible to make this a little more portable/dynamic though...can we not substitue the domain for a SERVER variable of the current domain?

Re: .htaccess redirect to domain

Posted: Fri Aug 14, 2009 6:12 pm
by thewebhostingdir
This is what I am using for my domain:

RewriteCond %{HTTP_HOST} ^amrita-rao.in$ [OR]
RewriteCond %{HTTP_HOST} ^www.amrita-rao.in$
RewriteRule ^/?$ "http\:\/\/www\.amrita\-rao\.in" [R=301,L]

Re: .htaccess redirect to domain

Posted: Sat Aug 15, 2009 9:43 pm
by redmonkey
*If* you have the 'UseCanonicalName' directive set to On then it's a simple case of....

Code: Select all

RewriteCond   %{HTTP_HOST} !^www\.                             [NC]
RewriteRule   .*           http://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
.. and if you don't the above will get stuck in an endless loop.

This is based on your rule being set in your main (or virtual host) config section, not sure how it would react when used in a directory .htaccess file. It also assumes that your 'ServerName' is set correctly.