我不能使用 htaccess 进行超过一定数量的重定向吗?
我已经注册了 3 个域名,全部用于同一个网站。我这样做是为了抓住那些记不住正确网址的人。 所以我有:
- 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:
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第二个
RewriteCond
仅检查主机名开头是否以(www.)domain.org
开头,因此在重定向到domain.org.au
。这将导致无限次数的重定向,导致浏览器在一定次数的尝试后放弃。您真正需要的是匹配
(www.)domain.org(END)
。美元符号$
表示正则表达式中的字符串结尾,如下所示:^(www\.)?domain\.com\.au$
表达式的工作方式如下this:^
= 字符串开头(www\.)
= "www."作为一个组?
= 前一组一次或零次domain\.com\.au
= domain.com.au (点通常表示“任何字符”,但是前面有反斜杠时则不然)$
= 字符串结尾因此,整个表达式的意思是:
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: