donglin5770
2014-06-29 22:58
浏览 54
已采纳

.htaccess无法捕获GET参数

.htaccess

RewriteRule ^apply/import?$ index.php?action=import [NC]
RewriteRule ^apply/import/([0-9a-zA-Z]+)/?$ index.php?action=import&code=$1 [NC]

I run the following url's and print_r($_GET)

"localhost/apply/import" //Prints Array ( [action] => import ) as expected

"localhost/apply/import?code=abc123" // Prints Array ( [action] => import ) only, and is missing code.

How do I catch get variable code?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudengshen5591 2014-06-29 23:09
    已采纳

    With "localhost/apply/import?code=abc123", the first rule is matched (you can check it by removing second rewrite code)
    If you want to trigger second rewrite rule, you should try something like localhost/apply/import/abc123 and this will be the result:

    Array ( [action] => import [code] => abc123)

    UPDATE: I think this can solve your problem:
    You can use [QSA] to get extra Query String parameters, in your first rewrite rule:

    RewriteRule ^apply/import?$ index.php?action=import [QSA]
    

    Then going to localhost/apply/import?code=abc123 will output this:
    Array ( [action] => import [code] => abc123)

    You can read more about RewiteRule Flags. Here is the explanation:

    When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题