htaccess mod 重写 - 如何创建虚拟子域?
我想通过 htaccess 创建虚拟子域,方法如下。
输入:
http://testuser.domain.com/1/2/3/
应处理为:
http://www.domain.com/user.php?id=testuser&var1=1&var2=2&var3=3
但是,此重写不应使用 user.php,而应使用 index.php,以防有人输入:
http://www.domain.com or http://domain.com
这是我到目前为止所得到的,但它似乎不起作用。 任何来自模组重写专家的帮助将不胜感激。 提前致谢。
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]
I would like to create virtual subdomains through htaccess in the following way.
Entering:
http://testuser.domain.com/1/2/3/
Should be processed as:
http://www.domain.com/user.php?id=testuser&var1=1&var2=2&var3=3
HOWEVER, this rewrite should not use user.php, but index.php, in case someone enters:
http://www.domain.com or http://domain.com
This is what I got so far, however it doesn't seem to work.
Any help from a mod rewrite expert would be much appreciated.
Thanks in advance.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ index.php?var1=$1&var2=$2&var3=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^(.*)/?(.*)/?(.*)/?$ user.php?id=%2&var1=$1&var2=$2&var3=$3 [NC,QSA,L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的规则实际上非常接近于您希望它们执行的操作。我看到的唯一问题是两个
RewriteRule
语句的测试模式。目前,您有......这恰好相当于:
这是因为第一个捕获组之后的所有内容都无法匹配任何内容,但仍然被视为匹配,因此第一个组是贪婪的并且匹配整个输入字符串而无需推迟到图案的其他部分。
由于捕获组无论如何都不应该捕获正斜杠,因为它们在此处用作变量分隔符,因此直接的解决方法是将它们更改为
[^/]*
,如下所示: >编辑:我还修改了第二组中设置的
RewriteCond
,以忽略/index.php<情况下的
!-f
条件/code>,如果您请求子域名而域名后没有任何内容,则会发生这种情况。Your rules are actually very close to doing what you want them to. The only problem that I can see is in your test patterns for your two
RewriteRule
statements. Currently, you have...which happens to be equivalent to this:
This is because everything past the first capture group can match nothing and still be considered a match, so that first group is greedy and matches the whole input string without needing to defer to the other parts of the pattern.
Since the capture groups shouldn't capture forward slashes anyway, as they're being used as a variable delimiter here, the straightforward fix is to change them to
[^/]*
, as so:Edit: I also modified the
RewriteCond
set in the second group to ignore the!-f
condition in the case of/index.php
, which will happen if you request the subdomain without anything after the domain.