不同的 .htaccess 重写规则取决于 $_GET 参数
Different .htaccess rewrite rules depending on a $_GET param
如果传递了某个 GET
参数,是否有办法在 .htacess
中制定不同的重写规则?
例如,如果查询字符串是:
htttp://domain.com/?debug=1
,.htaccess
应该是这样的:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/ [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? [QSA,L]
您可以这样使用 RewriteCond
:
RewriteEngine on
# ?debug=1 is present
RewriteCond %{QUERY_STRING} (^|&)debug=1\b [NC]
RewriteRule (.*) app/webroot/ [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? [QSA,L]
您想匹配 RewriteCond 中的查询字符串:
RewriteCond %{QUERY_STRING} ^debug=1$ [NC]
RewriteRule (.*) app/webroot/ [L]
有关详细信息,请参阅 docs or this example。
如果传递了某个 GET
参数,是否有办法在 .htacess
中制定不同的重写规则?
例如,如果查询字符串是:
htttp://domain.com/?debug=1
,.htaccess
应该是这样的:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/ [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? [QSA,L]
您可以这样使用 RewriteCond
:
RewriteEngine on
# ?debug=1 is present
RewriteCond %{QUERY_STRING} (^|&)debug=1\b [NC]
RewriteRule (.*) app/webroot/ [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? [QSA,L]
您想匹配 RewriteCond 中的查询字符串:
RewriteCond %{QUERY_STRING} ^debug=1$ [NC]
RewriteRule (.*) app/webroot/ [L]
有关详细信息,请参阅 docs or this example。