2015-09-09 37 views
4

如果传递某个GET参数,是否有方法在.htacess中制定不同的重写规则?

例如,如果查询字符串是:

htttp://domain.com/?debug=1,比.htaccess应该是这样的:

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule ^$ app/webroot/ [L] 
    RewriteRule (.*) app/webroot/$1 [L] 
</IfModule> 

如果在查询字符串没有debug=1,比:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 

RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L] 
+0

你的问题是什么? – starkeen

+0

@Starkeen我问过是否可以实现我在文章中描述的构造。其实,我的第一句话是个问题。我会再次重复一遍:有没有一种方法可以根据请求GET参数制定不同的重写规则。 –

+0

你的意思是说,如果在URL中有查询字符串,那么它应该重写为index.php,否则app/webroot /? – starkeen

回答

3

你可以这样使用RewriteCond

RewriteEngine on 

# ?debug=1 is present 
RewriteCond %{QUERY_STRING} (^|&)debug=1\b [NC] 
RewriteRule (.*) app/webroot/$1 [L] 

# ?debug=1 is not present 
RewriteCond %{QUERY_STRING} !(^|&)debug=1\b [NC] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l  
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L] 
+1

谢谢,我会试试它。 –

+0

似乎'QUERY_STRING'没有被编码。我需要一些Apache配置吗? –

+0

'QUERY_STRING'适用于所有mod_rewrite版本。哪个网址不适合你? – anubhava

2

你要匹配的的RewriteCond查询字符串:

RewriteCond %{QUERY_STRING}  ^debug=1$  [NC] 
RewriteRule (.*) app/webroot/$1 [L] 

欲了解更多信息,请参阅docsthis example