开发者

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.

  • 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 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."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜