URL Rewrite escape needs help.RSS

5 replies

Last post Dec 30, 2020 08:06 AM by Brucz

  • URL Rewrite escape needs help.

    Dec 27, 2020 11:16 PM|ourtoms|LINK

    Hello!

    This URL can be used.

    http://localhost/search.php?searchkey=字符串&searchtype=all&page=2


    After rewriting, it cannot be used:


    http://localhost/search/all/%D3%CE%CF%B7/2.html


    My rules:

    ------------------------------------------------------
    <rule name="UrlEncode example0" stopProcessing="true">
          <match url="^search/all/([^/]+)/([0-9]+)\.html$" ignoreCase="false" />
    <conditions>
    <add input="{UrlDecode:{REQUEST_URI}}" pattern="search/all/([^/]+)/([0-9]+)\.html" />
    </conditions>
    <action type="Rewrite" url="/search.php?searchkey={C:1}&amp;searchtype=all&amp;page={R:2}" appendQueryString="false" />
    </rule>


    or 


    <rule name="UrlEncode example1" stopProcessing="true">
    <match url="^search/all/([^/]+)/([0-9]+)\.html$" ignoreCase="false" />
    <action type="Rewrite" url="/search.php?searchkey={UrlEncode:{R:1}}&amp;searchtype=all&amp;page={R:2}" appendQueryString="false" />
    </rule>

    ------------------------------------------------------


    Two rules cannot be used,

    Special characters cannot be escaped.


    What's wrong? Searchkey = string, request character is Chinese character, and it will be automatically escaped after submission %D7%D6%B7%FB%B4%AE


    In Linux nginx PHP, it's normal:

    RewriteRule ^search/all/([^/]+)/([0-9]+)\.html$  /search.php?&searchkey=$1&searchtype=all&page=$2  [L]


    Please help me, thank you!

  • Re: URL Rewrite escape needs help.

    Dec 28, 2020 03:58 AM|Brucz|LINK

     Hi ourtoms,

    I think it is not a problem of IIS url rewrite, but php.

    This is the rule I used to test and it is similar to yours.

      <rule name="urlencode">
          <match url="^search/all/([^/]+)/([0-9]+)\.html$" />
          action type="Rewrite" url="/search.php?searchkey={R:1}&amp;amp;searchtype=all&amp;amp;page={R:2}" />
       </rule>

    When I set search.php, the Chinses character escaped to %D7%D6%B7%FB%B4%AE, but php cannot recognize %D7%D6%B7%FB%B4%AE. Even I access it through http://localhost/search.php?searchkey=汉字&searchtype=all&page=2 directly, url escape chinese character to %D7%D6%B7%FB%B4%AE by browser.

    If I enter %D7%D6%B7%FB%B4%AE in browser, or change it to other, it became this.

    Browser can recognize and escaped it to 汉孔 but php cannot.

    If I change search.php to search.aspx, there is no problem.

    And in fail request tracing, it shows like this.

    So it is not caused by url rewrite, you should check the php code about how to escape %D7%D6%B7%FB%B4%AE to chinese character.

    Best regards,

    Brucz

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: URL Rewrite escape needs help.

    Dec 29, 2020 12:40 AM|ourtoms|LINK

    Hello!

    Thank you for your reply

    My file is encrypted by CMS, and I don't have permission to modify it,

    and

    When I run in CentOS8.3 / nginx 1.19.5/ php7.4   it is normal.

    Therefore, it should be the problem of IIS rewriting rules.

  • Re: URL Rewrite escape needs help.

    Dec 29, 2020 07:18 AM|Brucz|LINK

     Hi ourtoms,

    From screenshort, you can see that  browser escape chinese character, not IIS url rewrite. 

    Asp.net can escape encode to chinese character but php cannot. 

    Now, the latest version of php IIS can support is 7.4, and IIS will not support newer version of php in the future.

    Best regards,

    Brucz

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: URL Rewrite escape needs help.

    Dec 29, 2020 06:37 PM|ourtoms|LINK

    Hi,

    Sorry, I may not have made it clear,

    My PHP file has automatically escaped URL encoding,

    http://localhost/search.php
    The first search results page, in which the tab section is normal.

    Use pseudo static on the next page of the search results page:

    In the HTML source code is:


    The original URL is:

    <a href="http://localhost/search.php?searchkey=%D3%CE%CF%B7&searchtype=all&page=2" target="_ blank">Next</a>

    URL Rewriting:

    <a href="http://localhost/search/all/%D3%CE%CF%B7/2.html" target="_ blank">Next</a>

    Where  %D3%Ce%CF%B7 is transferred to database search,

    In the returned page, the result is garbled,

    So you need to decode %D3%Ce%CF%B7 transfer it to the database for query,

    So it's not automatic transcoding in the sense of browser.

  • Re: URL Rewrite escape needs help.

    Dec 30, 2020 08:06 AM|Brucz|LINK

    Hi ourtoms,

    Generally speaking, url cannot pass special characters, it will be escaped into an encoding string, in addition to Chinese characters, there are special symbols like %#. Only letters can be passed directly.

    If Chinese characters are keywords used for searching in the database in your application, I suggest you re-encode them into Chinese characters within the application and then search the database. The URL specification does not recommend that Chinese be part of it.

    When the URL transmits Chinese characters, it will be escaped into encoding, then you can directly decode the received parameters in the method corresponding to the request, and then send it to the database for query.

    In addition, you can see from the record of fail request tracing that the Chinese characters in the rewritten URL still exist, and url rewrite did not escape it.

    Best regards,

    Brucz

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.