我不能使用 htaccess 进行超过一定数量的重定向吗?

发布于 2024-08-24 08:09:01 字数 1235 浏览 6 评论 0原文

我已经注册了 3 个域名,全部用于同一个网站。我这样做是为了抓住那些记不住正确网址的人。 所以我有:

  1. domain.com.audomain.orgdomain.org.au

托管位于#1 下,#2 和#3 作为停放

  • 我希望每个人都能定向到 #3 (domain.org.au),因为它是澳大利亚的非营利慈善机构网站。
  • 我正在使用 Wordpress。在 WordPress 管理设置中,我已将站点设置为在域的根目录下可见,该目录创建了两个 .htaccess 文件:一个在根目录中,一个在 wordpress 文件夹中。
  • 我正在编辑的文件位于根目录中,目前看起来像这样:
 
    # 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

我的第一个重定向工作正常,但是当我添加 .org -> .org.au 浏览器卡住并说我必须进行多次重定向。这是可能的,这是我第一次涉足 .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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-08-31 08:09:01

您的第二个 RewriteCond 仅检查主机名开头是否以 (www.)domain.org 开头,因此在重定向到 domain.org.au。这将导致无限次数的重定向,导致浏览器在一定次数的尝试后放弃。

您真正需要的是匹配 (www.)domain.org(END) 。美元符号 $ 表示正则表达式中的字符串结尾,如下所示:

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]

^(www\.)?domain\.com\.au$ 表达式的工作方式如下this:

  • ^ = 字符串开头
  • (www\.) = "www."作为一个组
  • ? = 前一组一次或零次
  • domain\.com\.au = domain.com.au (点通常表示“任何字符”,但是前面有反斜杠时则不然)
  • $ = 字符串结尾

因此,整个表达式的意思是:

完全是“domain.com.au”,没有其他字符,可以选择在前面加上“www”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文