.htaccess文件中的mod_rewrite为未知path的第一部分

我想写一个重写规则来改变pathslu to查询参数。 这是一个Web服务,只应该重写这个规则,如果主机启动API。 有两个slu that,我试图捕捉和重写。 第一个是可选的,是一个版本(即v1.2),第二个是服务域(即客户,交易等)。

http://api.domain.com/v2.5/customers应该重写为?version = 2.5&domain = customers

我也想支持一个默认版本

http://api.domain.com/customers应该重写为?version =&domain = customers

这是我的.htaccess文件的样子:

RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^api\..* RewriteRule ^v([\d\.]*)?\/?([^\/]*)$ ?version=$1&domain=$2 

上面的第一个例子工作正常,但我不能得到默认的版本path工作。 我尝试了很多不同的东西。 我认为以^。* v开头会有所帮助,但是没有。 当你不知道起始字符时,谁知道如何使它匹配?

尝试这个:

 # Handle the version-including requests first.. RewriteCond %{HTTP_HOST} ^api\..* RewriteRule ^v([\d\.]*)/([^/]*)$ ?version=$1&domain=$2 [L] # ..then catch requests that don't include a version. RewriteCond %{HTTP_HOST} ^api\..* RewriteRule ^([^/]*)$ ?version=&domain=$1 [L] 

它可以在一个正则expression式中完成,但是这样可读性更强。