How to 301 redirect Non-WWW to WWW version - Search Marketing Arena most recent 30 from http://www.searchmarketingarena.com 2010-09-09T09:58:08Z http://www.searchmarketingarena.com/feeds/question/243 http://www.creativecommons.org/licenses/by-nc/2.5/rdf http://www.searchmarketingarena.com/questions/243/how-to-301-redirect-non-www-to-www-version How to 301 redirect Non-WWW to WWW version phpsys 2009-10-30T15:24:02Z 2010-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#245 Answer by franseo for How to 301 redirect Non-WWW to WWW version franseo 2009-10-30T17:35:04Z 2009-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#250 Answer by weppos for How to 301 redirect Non-WWW to WWW version weppos 2009-11-01T10:45:58Z 2009-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#263 Answer by must for How to 301 redirect Non-WWW to WWW version must 2009-11-04T14:13:31Z 2009-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#471 Answer by Gerry Williams for How to 301 redirect Non-WWW to WWW version Gerry Williams 2009-12-31T22:12:04Z 2009-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>