AFRC
AFRC

Reputation: 914

htaccess redirect all content including subdomains

I have 3 domains, for the same web app, since I don't want to have duplicated content I want to redirect the other two domains to the primary domain.

If a user types client_xyz.domain2.com it should be redirected to client_xyz.primarydomain.com.

If a user types client_xyz.domain2.com/folder/file/etc it should be redirected to client_xyz.primarydomain.com/folder/file/etc.

If a user types domain2.com/test/page it should be redirected to primarydomain.com/test/page

I thought this is the best solution to avoid Google penalty for duplicated content. If you think there's a better solution to deal with this (eg. DNS), let me know.

Upvotes: 0

Views: 191

Answers (3)

anubhava
anubhava

Reputation: 784888

Put this code in your .htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteOptions MaxRedirects=10

RewriteCond %{HTTP_HOST} ^(client_xyz\.)?domain2\.com$ [NC]
RewriteRule ^ http://%1primarydomain.com%{REQUEST_URI} [NE,R=301,L]

Upvotes: 2

Grim...
Grim...

Reputation: 16953

If you can use mod_rewrite, this should work:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (^.*?\.|^)?domain2\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.primarydomain.com [R=301,NC,L]

If not, well, I'm probably going to have to leave it up to someone smarter than me ;-)

Upvotes: 0

Grim...
Grim...

Reputation: 16953

You can user regex in the .htaccess file, so set up a 301 (moved permanently) redirect.

RedirectMatch 301 (^.*?\.|^)domain2.com(.*) $1.primarydomain.com$2

Untested, but should be fine.

[edit]The below is fairly useless since your comment, but I'll leave it here anyway.

Alternatively, you could set up server alias's in your Apache config file. You probably have something like this:

ServerName primarydomain.com
DocumentRoot /var/www/html/mysite/
<Directory "/var/www/html/mysite/">
    AllowOverride All
</Directory>

Change it to:

ServerName primarydomain.com
ServerAlias domain2.com
DocumentRoot /var/www/html/mysite/
<Directory "/var/www/html/mysite/">
    AllowOverride All
</Directory>

Upvotes: 0

Related Questions