htaccess的mod-rewrite - 如何创建虚拟子域? [英] htaccess mod rewrite - how to create virtual subdomains?

查看:132
本文介绍了htaccess的mod-rewrite - 如何创建虚拟子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下方式创建通过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或http://domain.com
 

这是我得到了什么,到目前为止,但它似乎并没有工作。 从mod-rewrite专家提供任何帮助将非常AP preciated。 先谢谢了。

 选项+了FollowSymLinks
选项​​+指标
RewriteEngine叙述上
的RewriteBase /

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{HTTP_HOST} ^(WWW \)?domain.com $ [NC]
重写规则^ / / / $的index.php VAR1 = $ 1安培(*)?(*)?(*);?VAR2 = $ 2及VAR3 = $ 3 [NC,QSA,L]

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{HTTP_HOST} ^!(WWW |邮件).domain.com $ [NC]
的RewriteCond%{HTTP_HOST} ^(WWW \。)([A-Z0-9  - ] +)?。domain.com $ [NC]
(。*)?(。*)?(。*)?重写规则^ / / / $ user.php的编号=%2及?VAR1 = $ 1和; VAR2 = $ 2及VAR3 = $ 3 [数控,QSA,L]
 

解决方案

您的规则实际上是非常接近做你想让他们。我能看到的唯一的问题是在你的测试图案为你的两个重写规则语句。目前,您有

 重写规则^(。*)/(。*)/(。*)/ $ ...
 

...这恰好是相同的:

 重写规则^(。*)$
 

这是因为一切都会过去的第一个捕获组可以搭配什么,仍然被认为是一个比赛,让第一组是贪婪和整个输入字符串,而无需推迟到图案的其他部分相匹配。

由于捕获组不应该捕获正斜杠反正,因为它们可以被用来作为这里的变量分隔符,直截了当的解决方法是将其更改为 [^ /] * ,像这样:

修改:我还修改了的RewriteCond 第二组设置为忽略 - ˚F状况的index.php ,如果你要求的子域没有域名,任何事情都会发生。

 的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{HTTP_HOST} ^(WWW \)?domain.com $ [NC]
?重写规则^([^ /] *)/([^ /] *)/([^ /] *)/ $的index.php VAR1 = $ 1安培;???VAR2 = $ 2及VAR3 = $ 3 [NC, QSA,L]

的RewriteCond%{} REQUEST_FILENAME!-f [OR]
的RewriteCond%{REQUEST_URI} ^ / index.php文件
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{HTTP_HOST} ^!(WWW |邮件).domain.com $ [NC]
的RewriteCond%{HTTP_HOST} ^(WWW \。)([A-Z0-9  - ] +)?。domain.com $ [NC]
重写规则^([^ /] *)/([^ /] *)/([^ /] *)/ $ 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]

解决方案

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

RewriteRule ^(.*)/?(.*)/?(.*)/?$ ...

...which happens to be equivalent to this:

RewriteRule ^(.*)$

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.

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 [OR]
RewriteCond %{REQUEST_URI} ^/index.php
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]

这篇关于htaccess的mod-rewrite - 如何创建虚拟子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆