htaccess的动态子域重写重写、动态、htaccess

2023-09-02 10:02:57 作者:倾城容颜也抵不过流年易逝

我发誓我一直在寻找小时,这里的答案,但它仍然没有工作。这是我有:

I promise I have been looking for hours for the answer here, but it's still not working. Here's what I have:

RewriteCond %{HTTP_HOST} ^([^.]*).example.com [NC]
RewriteCond %{REQUEST_URI} !^/%1/ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

这将创建一个无限循环,因为二线是不正确的匹配。不过,如果我替换%1 两行与 WWW ,进入 www.example .COM ,它工作正常。很明显,它不再对其他子域的作品让,但是,当我明确地进入事实 WWW 两行,而不是%1 照顾它很奇怪,因为我知道的值%1 WWW

This creates an infinite loop because line two is not matching correctly. However, if I replace %1 in line two with www and go to www.example.com, it works fine. Obviously it no longer works for the other subdomains, but the fact that when I explicitly enter www in line two instead of letting %1 take care of it is strange, because I know the value of %1 is www.

我试图完成如下: 我有一个域example.com。在根目录下有几个子目录,比方说,开发,测试和WWW。高在我的htaccess的文件我有强迫域名重定向到www.example.com如果子域是空的(example.com),W(w.example.com),或WW(ww.example规则.COM),其它任何被单独留在家中(dev.example.com,test.example.com)。现在这部分的文件应该重写请求基于所述子域的正确子目录。所以,像dev.example.com/something.jpg的请求被重写(不定向),以dev.example.com/dev/something.jpg。

What I'm trying to accomplish is as follows: I have a domain "example.com". In the root directory there are several subdirectories, let's say "dev", "test" and "www". Higher up in my htaccess file I have a rule that forces the domain to redirect to "www.example.com" if the subdomain is empty (example.com), w (w.example.com), or ww (ww.example.com), anything else is left alone (dev.example.com, test.example.com). Now this part of the file is supposed to rewrite the request to the correct subdirectory based on the subdomain. So, a request like "dev.example.com/something.jpg" gets rewritten (not redirected) to "dev.example.com/dev/something.jpg".

这里的计划是要能够在服务器上创建一个开发和测试环境,而无需重新创建我的家庭服务器上的环境中,并确保所有的服务器设置是完全一样的。此外,它可以做任何事情,我想它做的事,我想,但这是燃眉之急。

The plan here is to be able to create a development and testing environments on the server, without having to recreate an environment on my home server, making sure all the server settings are exactly the same. Also, it could do anything else I wanted it to do I suppose, but that's the immediate need.

推荐答案

现在的问题是,你不能在一个的RewriteCond 。这是可行的:

The problem is that you can't use a backreference variable in the match expression of a RewriteCond. This is OK:

# ----------V
RewriteCond %1 <regex>

但是,这并不:

But this is not:

# -------------------------V
RewriteCond %{REQUEST_URI} %1

东西,如果你需要做一个像这样的比赛,你可以做的是使用匹配反向引用 1 从内部同样经常EX pression访问比赛。然后,您可以把%1 返回引用的RewriteCond 的同左侧的参数,并使用 1 来匹配它。事情是这样的:

Something you could do if you needed to do a match like this is use match backreference 1 to access a match from within the same regular expression. You can then put the %1 back reference in the same left side parameter of RewriteCond and use 1 to match against it. Something like this:

RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:1 [NC]

因此​​,%{REQUEST_URI}:%1 位会是这个样子: /富:子域,和 ^ /([^ /] +)[^:] *: 1 不匹配,因为前pression正在寻找的富后:( 1 =富)。因此,该规则被应用和URI现在 /子域/富,而第二次左右,%{REQUEST_URI}:%1 变成 /子域/富:子域,这一次的定期EX pression匹配,因为 1 =子域和使它如此规则的不的得到应用。

So the %{REQUEST_URI}:%1 bit could look something like this: /foo:subdomain, and ^/([^/]+)[^:]*:1 won't match because the expression is looking for foo after the ":" (1 = "foo"). So the rule gets applied and the URI is now /subdomain/foo, and the second time around, %{REQUEST_URI}:%1 becomes /subdomain/foo:subdomain, this time the regular expression matches because 1 = "subdomain" and the ! makes it so the rule doesn't get applied.