301 redirect on same hosting server

user221193 asked:

I originally bought the domain http://example.com with a hosting package and have run a WordPress blog on that for a while.

Now I’ve bought an added on domain http://example.com.au and have duplicated the content but now want to write a 301 redirect.

As the new domain is an addon to the original server, it shares the .htaccess file and the general 301 redirect I’ve implemented has resulted in an infinite redirect loop.

What is the correct way to do this?

I attempted to follow instructions on an earlier thread posted here.

Essentially inserted this:

Options +FollowSymLinks
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^www.example.com [NC] 
RewriteRule (.*) http://www.example.com.au/$1 [R=301,L]

My answer:

Here is the problem:

RewriteCond %{HTTP_HOST} ^www.example.com [NC]

This regex isn’t terminated, so it matches both www.example.com and www.example.com.au.

To fix the problem, terminate the regex.

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.