Can’t I have more than a certain number of redirects using htaccess?

I’ve registered 3 domains, all for the same site. I’ve done this to catch people who can’t remember the right URL.
So I’ve got:

  1. domain.com.au
  2. domain.org
  3. domain.org.au

The hosting is under #1, with #2 and #3 as parked domains.

Read More
  • I want everybody to get directed to #3 (domain.org.au) because it is a site for non-profit charity in Australia.
  • I’m using WordPress. Within the WordPress admin settings I’ve set the site to be visible at the root of the domain, which has created two .htaccess files: one in root, and one in the wordpress-folder.
  • The file I’m editing is in the root, and currently look like this:
 
    # BEGIN WordPress
    IfModule mod_rewrite.c> # deliberate missing open tag to show this line here
    RewriteEngine On
    RewriteBase /                       # from WordPress
    RewriteCond %{REQUEST_FILENAME} !-f # from WordPress
    RewriteCond %{REQUEST_FILENAME} !-d # from WordPress
    RewriteRule . /index.php [L]        # from WordPress
    # Point all domains to .org.au
    RewriteCond %{HTTP_HOST} ^(www.)?domain.com.au [NC]
    RewriteRule ^(.*)$ http://domain.org.au/$1 [R=301,L]
    # RewriteCond %{HTTP_HOST} ^(www.)?domain.org [NC]
    # RewriteRule ^(.*)$ http://domain.org.au/$1 [R=301,L] 
    /IfModule> # deliberate missing close tag to show this line here    
    # END WordPress

My first redirect works fine, but when I add the .org -> .org.au the browser chokes and says I have to many redirects. Which is possible, this is my first foray into .htaccess. So – what am I doing wrong?

Related posts

Leave a Reply

1 comment

  1. Your second RewriteCond only checks whether the hostname begins with (www.)domain.org, so it will still match after a redirect to domain.org.au. This will cause an infinite number of redirects, causing your browser to give up after a certain number of tries.

    What you really need is to match (www.)domain.org(END) instead. The dollar sign $ represents end-of-string in regular expressions, like so:

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

    The ^(www.)?domain.com.au$ expression works like this:

    • ^ = beginning of string
    • (www.) = “www.” as a group
    • ? = the previous group either one or zero times
    • domain.com.au = domain.com.au (dots normally mean “any character”, but not when they are preceded by backslash)
    • $ = end of string

    So, the entire expression means:

    exactly “domain.com.au” and no other characters, optionally preceded by “www.”