Match Question Mark in mod_rewrite rule regex

前端 未结 3 665
一整个雨季
一整个雨季 2020-11-30 06:25

I am looking to rewrite urls with multiple substrings. One substring is being requested as a subdirectory, while any others are requested as normal query string parameters.

相关标签:
3条回答
  • 2020-11-30 07:12

    You should be using the [QSA] flag instead of trying to rewrite the query string. [QSA] passes on the query string to the rewritten URL.

    So your rule should look like:

    ...
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([A-Za-z0-9-_]+)/friends/? friends.php?user=$1 [QSA,L]
    

    Your case is very similar to the example given for using the QSA flag in the mod_rewrite cookbook.

    0 讨论(0)
  • 2020-11-30 07:23

    The query is not part of the URL path and thus cannot be processed with the RewriteRule directive. This can only be done with the RewriteCond directive (see %{QUERY_STRING}).

    But as Chad Birch already said it suffices th set the QSA flag to automatically get the original requested query appended to the new URL.

    0 讨论(0)
  • 2020-11-30 07:28

    In addition to using the rewrite flag QSA, you can also use the QUERY_STRING environment variable as shown below:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^([A-Za-z0-9-_]+)/friends$ /friends.php?user=$1&%{QUERY_STRING}
    

    And the URL in question

    http://www.example.com/mark/friends?page=2
    

    will be rewritten to (as specified):

    http://www.example.com/friends.php?user=mark&page=2
    
    0 讨论(0)
提交回复
热议问题