Requiring the WWW

Search Engine Optimization using Htaccess

Requiring the WWW

Postby produke » 25 Sep 2006 13:45

If you would like to redirect anything from http://domain.com to http://www.domain.com (so the www is always in the URL), you can accomplish this by using the code below. This is helpful in search engine optimization and will help give your site a higher page rank.

I use the first method, but only when I can't edit the httpd.conf file directly.

Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# require non-empty HOST header
RewriteCond %{HTTP_HOST} !^$

# require case-insensitive HOST to be www.htaccesselite.com
RewriteCond %{HTTP_HOST} !^www\.htaccesselite\.com$ [NC]

# 301 redirect everything to correct www.htaccesselite.com
RewriteRule ^(.*)$ http://www.htaccesselite.com/$1 [R=301,L]

# or you can use this rewriterule
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]



Code: Select all
RewriteCond %{HTTP_HOST} !^www\.htaccesselite\.com$
RewriteRule ^(.*)$ http://www.htaccesselite.com/$1 [R=301,L]


Code: Select all
RewriteCond %{HTTP_HOST} ^htaccesselite\.com$ [NC]
RewriteRule ^(.*) http://www.htaccesselite.com/$1 [QSA,L,R=301]



Code: Select all
RewriteCond %{HTTP_HOST} !^www\.htaccesselite\.com$ [NC]
RewriteRule ^(.*) http://www.htaccesselite.com/$1 [QSA,L,R=301]



Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\..* [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
Last edited by produke on 30 May 2007 08:40, edited 2 times in total.
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

Postby ti89 » 30 Oct 2006 20:29

For sites running on a port other than 80:
Code: Select all
RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*)         http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R=301]




And for a site running on port 80
Code: Select all
RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://fully.qualified.domain.name/$1 [L,R=301]
ti89
 
Posts: 44
Joined: 28 Oct 2006 07:37

Postby produke » 04 Dec 2006 13:29

And for those of you on a Windows Server using ISAPI_Rewrite, it's as simple as adding this to your .ini file...
Code: Select all
RewriteCond Host: ^example\.com
RewriteRule (.*) http\://www\.example\.com$1 [I,RP]



Code: Select all
#------------------------------------------
# First optional line, include only in case of errors:
RewriteEngine On
# Second optional line, include only in case of errors:
Options +FollowSymLinks
# Optional start tag, requires use of corresponding end tag as well
< IfModule mod_rewrite.c >
# ----------------- the real stuff starts here
# IF there's a host field at all, AND
RewriteCond %{HTTP_HOST} .
# IF domain does not start with www, AND
RewriteCond %{HTTP_HOST} !^www\.threadwatch
# IF subdomain is not another one of those you like
RewriteCond %{HTTP_HOST} !^sub1\.threadwatch [NC]
RewriteCond %{HTTP_HOST} !^sub2\.threadwatch [NC]
RewriteCond %{HTTP_HOST} !^sub3\.threadwatch [NC]
RewriteCond %{HTTP_HOST} !^sub4\.threadwatch [NC]
RewriteCond %{HTTP_HOST} !^sub5\.threadwatch [NC]
# THEN redirect everything to an appropriate location
RewriteRule (.*) http //www.threadwatch.org/$1 [R=301,L]
# ----------------- the real stuff ends here
# Optional end tag, only if you have used the optional start tag
< /IfModule >
#------------------------------------------
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

Re: Requiring the WWW

Postby olivia » 03 Dec 2008 03:06

Thanks for your info, but how to redirect my sub domain..........?
olivia
 
Posts: 7
Joined: 03 Dec 2008 02:49


Return to SEO



Who is online

Users browsing this forum: No registered users