在多主机环境中使用干净的URL获取params

时间:2012-10-05 11:21:14

标签: apache .htaccess url-rewriting clean-urls

网站根包含三个目录,a,b,c所有目录都充当不同域的根

我在目录.htaccess中创建了一个b,并向其添加了以下规则

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]*)/?([^/]*)/$ /b/sample-page?bbi=$1&bbl=$2 [L]

代替上面的最后一行,我也试过

RewriteRule ^b/([^/]*)/([^/]*)$ /b/sample-page?bbi=$1&bbl=$2 [QSA,L]

使用上述规则

http://testsite.com/b/pageone.php已成功重写为

http://testsite.com/b/pageone

但是http://testsite.com/b/sample-page.php?bbi=value1&bbl=value2

重写为http://testsite.com/b/sample-page?bbi=value1&bbl=value2而不是

http://testsite.com/b/sample-page/value1/value2

http://testsite.com/sample-page/value1/value2

1 个答案:

答案 0 :(得分:1)

您需要在其他规则中添加L标记:

RewriteRule ^(.*)$ $1.php [L]

要处理其他情况,您需要在之前添加规则。

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/b/([^/]+)/([^/]+)/([^/]+)
RewriteCond %{DOCUMENT_ROOT}/b/%1.php -f
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+) $1.php?bbi=$2&bbl=$3 [L]

这应该是这样的,当你转到http://testsite.com/b/sample-page/value1/value2时,你会收到/b/sample-page.php?bbi=value1&bbl=value2的内容