【问题标题】:Different .htaccess rewrite rules depending on a $_GET param不同的 .htaccess 重写规则取决于 $_GET 参数
【发布时间】:2015-12-05 03:24:58
【问题描述】:

如果传递了某个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]

【问题讨论】:

  • 你有什么问题?
  • @Starkeen 我问过是否有可能实现我帖子中描述的构造。实际上,我的第一句话是一个问题。我再重复一遍:有没有办法根据请求 GET 参数制定不同的重写规则。
  • 你的意思是如果url中有查询字符串那么它应该重写为index.php否则app/webroot/?
  • 是的,类似的。

标签: apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

您想与 RewriteCond 中的查询字符串匹配:

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

有关详细信息,请参阅docsthis example

【讨论】:

    【解决方案2】:

    你可以像这样使用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]
    

    【讨论】:

    • 似乎QUERY_STRING 没有被计算。我需要一些 apache 配置吗?
    • QUERY_STRING 适用于所有 mod_rewrite 版本。哪个网址不适合您?
    • take.ms/AqjqV 这是我的 .htaccess。这是我的网站:iamaplayer.ru。有一个die('a');在我的测试 php 中,但如果我添加命中 iamaplayer.ru/cup.html?debug=1,我就达不到它。我仍然可以浏览所有网站。
    • .htaccess 之后.htaccess 中仍有RewriteRule ^$ app/webroot/ [L]RewriteCond %{QUERY_STRING} !(^|&amp;)debug=1\b [NC],但正如您在上面提到的,我已经删除了该冗余规则。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2013-12-26
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多