Advice needed on rewrites

I have a client who has multiple domains, some are being indexed with AND without the www. The other issue is that they are all the same site. One of them has a different homepage, but all the content is running from the same server files. They also have two two folders, NEW folder and SERVICE AREAS folder, with identical files. This is causing an issue with their seo guy and the person who made the site for them doesn’t know how to fix it! So the seo guy asked me to just rewrite all the domains to the main one with www. So my plan of attack is this:

  1. Make two redirects for each domain, one redirecting the non-www and one redirecting the www, to the main domain with the www
  2. Redirect the NEW folder to the Service Ares folder.

My questions are:

  • do I have to write separate redirects for each domain twice, or is there a all inclusive way to do this?
  • can i redirect a folder? Like say to Or would I have to redirect every page within the folder?
  • and lastly, would these folder or individual page redirects need to be done for each of the four domains?
    Thanks, Scott

You can do this in one go. Either just redirect everything that is not the domain you want to the primary domain, or by using www as an optional atom

# pre-requisites for mod_rewrite
# needed for both methods
Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\\.primarydomain\\.com$ [NC]
RewriteRule .?{REQUEST_URI} [L,R=301]

# Method 2: Optional www
RewriteCond %{HTTP_HOST} !^(www\\.)?some-secondary-domain\\.com$ [NC]
RewriteRule .?{REQUEST_URI} [L,R=301]

Don’t use both methods at once. Pick one and stick with it.

Yes you can redirect a folder. The easiest way is to use

Redirect /newfolder

That depends on how it’s set up. If all domains are served from one directory you can apply method 1 from answer 1 just once. If they run from multiple directories you need to it apply it once for every directory.

Also, I would put the redirect before the other Rewrite, because otherwise you’d have to redirect twice, once to the correct domain, and then once to the correct directory. Whereas if you put it first it redirects to the correct domain and folder at the same time.

Ok thanks. So if I understand correctly… if I want all these,, (both www and non www) to go to main domain of AND forward that folder, my code would be like this:

Options +FollowSymlinks
RewriteEngine on

Redirect /newfolder

RewriteCond %{HTTP_HOST} !^www\.selectwaterproofingusa\.com$ [NC]
RewriteRule .?{REQUEST_URI} [L,R=301]

How does Apache know to redirect those other domains since they aren’t mentioned in the code?


Apache doesn’t have to know as you’ve indicated the files are co-located (parked domains). Therefore, there’s only one .htaccess to work with so Apache will do it all there. If they’re not co-located, just do the same thing in each DocumentRoot.