How to 301 redirect Non-WWW to WWW version - Search Marketing Arena most recent 30 from http://www.searchmarketingarena.com2010-09-09T09:58:08Zhttp://www.searchmarketingarena.com/feeds/question/243http://www.creativecommons.org/licenses/by-nc/2.5/rdfhttp://www.searchmarketingarena.com/questions/243/how-to-301-redirect-non-www-to-www-versionHow to 301 redirect Non-WWW to WWW versionphpsys2009-10-30T15:24:02Z2010-07-22T07:27:27Z
<p>What is your favorite way do redirect all pages from the Non-WWW to WWW version?
I know there are several different ways, I'd like to know how you do this in practice.</p>
<p>Code fragments and snippets welcomed.</p>
http://www.searchmarketingarena.com/questions/243/how-to-301-redirect-non-www-to-www-version/245#245Answer by franseo for How to 301 redirect Non-WWW to WWW versionfranseo2009-10-30T17:35:04Z2009-10-31T13:16:29Z<p>Hi, I prefer to use server with this cofiguration active by default, or enable by the plesk. In any case I say to Google by web master central tool what's my favourite version and if server do not support redirect by default I write some code on the .htaccess file (only on Apache webserver):</p>
<pre><code>Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
</code></pre>
http://www.searchmarketingarena.com/questions/243/how-to-301-redirect-non-www-to-www-version/250#250Answer by weppos for How to 301 redirect Non-WWW to WWW versionweppos2009-11-01T10:45:58Z2009-11-01T10:45:58Z<p>As usual, there are a couple of different way to accomplish this task.</p>
<p>First, let's discuss the use of Apache. Compared with passing the request to the backend language (in your case PHP), this is usually the preferred way. It's more lightweight and more fast because the webserver doesn't need to push additional levels to the execution stack.</p>
<p>You can write the directive in your Apache configuration file or in the .htaccess file. If you have access to Apache config file, this is the best place. Otherwise, the .htaccess is fine too.</p>
<p>The following directive redirects all request to <strong>any</strong> non-www host to the www-version of the site. </p>
<pre><code>RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [L,R=301]
</code></pre>
<p>I usually prefer the code above rather the following one that instead selective.y redirect a single domain to the www-version. (note the difference on the second line)</p>
<pre><code>RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [L,R=301]
</code></pre>
<p>Which one you should use depends on your specific case. If the former works, stick with it. Otherwise, if redirecting all host can cause some problem, use the latter.</p>
<p>If you want to use PHP, you can use the following code.</p>
<pre><code>$host = strtolower($_SERVER["HTTP_HOST"]);
if ($host != "www.domain.com") { // or $host == "domain.com"
$uri = $_SERVER["REQUEST_URI"];
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.domain.com$uri");
exit();
}
</code></pre>
http://www.searchmarketingarena.com/questions/243/how-to-301-redirect-non-www-to-www-version/263#263Answer by must for How to 301 redirect Non-WWW to WWW versionmust2009-11-04T14:13:31Z2009-11-04T14:13:31Z<p>if you have separate virtual hosts on your web server for non-www and www websites, you can simply put a line in .htaccess file on non-www vhost like this:
Redirect 301 / <a href="http://www.domain.tld/" rel="nofollow">http://www.domain.tld/</a></p>
http://www.searchmarketingarena.com/questions/243/how-to-301-redirect-non-www-to-www-version/471#471Answer by Gerry Williams for How to 301 redirect Non-WWW to WWW versionGerry Williams2009-12-31T22:12:04Z2009-12-31T22:12:04Z<p>The first version interfered with my host's system [fatcow.com] for access to the statistics files (Webalizer). Also it interfered (403) with the execution of my fommail script.</p>
<p>The second version fixed both problems.</p>
<p>Gerry cadrewebworks.com</p>