Skip to content

Inconsistency with Apache when handling url params during rewrite #38

@yezhiyi9670

Description

@yezhiyi9670

To reproduce

  • $webroot/

    • index.php

      <?php
      
      header('Content-Type: application/json');
      echo json_encode($_GET);
      
    • .htaccess

      RewriteEngine On
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ index.php [L]
      

Test:

  • https://example.com/?a=1{"a":"1"}
  • https://example.com/cccc?a=1{}

Expected behavior

The behavior above makes sense logically, since the URI is rewritten to index.php, with no query string, so the url params should not be preserved. But it seems that Apache will preserve the url params in this case. In fact, many applications (including Wordpress) use something like RewriteRule ^ index.php [L] and assume that the url params will be preserved.

(Workaround: RewriteRule ^ index.php?%{QUERY_STRING} [L]. #36 was discovered exactly due to this workaround)

I do not know the exact mechanism of Apache very clearly.

  • Will RewriteRule ^.*? index.php [L] also preserve the url params?
  • If the destination already contains a query string (RewriteRule ^ index.php?a=4&b=3 [L]), will the original url params be still preserved? If so, how will they be merged with the new ones specified in the query string?

If you intend to change the behavior, please research carefully.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions