Domain Empire

301 Redirect for no-www to www

Spaceship Spaceship
Watch
Impact
13
Ok, I have a virtual host account with one Primary Domain in the Root folder and several Secondary Domains in sub-folders.

For consolidation of Google Page Ranking and avoidance of any possible duplicate content problems with referrals to Domain.com vs www.Domain.com, I have made a master .htaccess file that I copy to EACH Domain's folder to 301 Redirect any reference to Domain.com to www.Domain.com.

It all works fine. I currently have it coded as:

RewriteEngine on
Options +ExecCGI
rewritecond %{http_host} ^Domain1.com [nc]
rewriterule ^(.*)$ http://www.Domain1.com/$1 [r=301,nc]

rewritecond %{http_host} ^Domain2.com [nc]
rewriterule ^(.*)$ http://www.Domain2.com/$1 [r=301,nc]

rewritecond %{http_host} ^Domain3.com [nc]
rewriterule ^(.*)$ http://www.Domain3.com/$1 [r=301,nc]
...Etc.

I suspect with the proper wild-card coding, I could consolidate the multiple redirect directives into one that would say "redirect [AnyDomain].com to www.[AnyDomain].com", but I am not sure how to code this correctly. Since all the sites are live, I don't want to screw it up.

Anyone out there know how to code that properly???
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
0
•••
PolurNET said:
You'll need a separate .htaccess for each domain, even if they are 'virtually' setup as subfolders.

See http://enarion.net/web/apache/htaccess/redirect-www-and-no-www/ for the best way to code this
Thanks, Polur. I DO have an .htaccess file in each domain, but what I wanted was to consolidate the code into one global statement in EACH .hataccess file, such that I would not have to add a statement for each domain everytime I added a new site.

I actually found the code elsewhere:

Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,NC]
Now, that one set of statements works for ALL domains, and I don't have to update it each time I add a new site. But yes, it has to be in an .htaccess file in EACH domain's folder.
 
0
•••
ArtfulWebSites said:
PolurNET said:
You'll need a separate .htaccess for each domain, even if they are 'virtually' setup as subfolders.

See http://enarion.net/web/apache/htaccess/redirect-www-and-no-www/ for the best way to code this
Thanks, Polur. I DO have an .htaccess file in each domain, but what I wanted was to consolidate the code into one global statement in EACH .hataccess file, such that I would not have to add a statement for each domain everytime I added a new site.

I actually found the code elsewhere:

Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,NC]
Now, that one set of statements works for ALL domains, and I don't have to update it each time I add a new site. But yes, it has to be in an .htaccess file in EACH domain's folder.

I'd love to know how to do this to redirect no-www to www.
 
0
•••
Here is .htaccess code for non-www to www

Here is what is working for me to do 301 (permanent) redirect of non-www access to www:

Code:
###
###  Assign all links to www.domain.ext
###
###  ***NOTE***  Reirects ALL Domain.ext to www.Domain.ext  
###              (even: sub.Domain.ext to www.sub.Domain.ext)
###              ( which may NOT be desirable!              )
###
RewriteEngine on
Options +ExecCGI
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,NC]
 
0
•••
ArtfulWebSites said:
Here is what is working for me to do 301 (permanent) redirect of non-www access to www:

Code:
###
###  Assign all links to www.domain.ext
###
###  ***NOTE***  Reirects ALL Domain.ext to www.Domain.ext  
###              (even: sub.Domain.ext to www.sub.Domain.ext)
###              ( which may NOT be desirable!              )
###
RewriteEngine on
Options +ExecCGI
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,NC]

I am sorry. my mistake. What I meant was how to redirect www to no-www? like www.domain.com to domain.com. Thanks

Just to add I am currently doing this:

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

But as you can see I will have to change the domain for each website. So I am looking for a general way to do this that I can copy and paste without modification just like you :) . I tried this but it leads to infinite loops:

Code:
RewriteCond %{HTTP_HOST} ^www
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,NC,L]
 
0
•••
edit; Ok I figured it out. To redirect from www to no-www you can use the following general code:
Code:
#redirect from www to no-www

RewriteCond %{HTTP_HOST} ^www.(.*)$  [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
 
0
•••
You can 301 redirect your parked domain to the main domain. This is the best way to prevent duplicate content. If you are using cpanel this can be done automatically using the addon-domain option, or you can do it easily through .htaccess if your provider not allow addon-domains.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back