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:
- domain.com.au
- domain.org
- domain.org.au
The hosting is under #1, with #2 and #3 as parked domains.
- 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?
Your second
RewriteCond
only checks whether the hostname begins with(www.)domain.org
, so it will still match after a redirect todomain.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:The
^(www.)?domain.com.au$
expression works like this:^
= beginning of string(www.)
= “www.” as a group?
= the previous group either one or zero timesdomain.com.au
= domain.com.au (dots normally mean “any character”, but not when they are preceded by backslash)$
= end of stringSo, the entire expression means: