How to pass a Persian string as a argument in the URL?

后端 未结 2 1775
無奈伤痛
無奈伤痛 2020-12-18 00:49

I have a URL like this pattern:

www.example.com/ClassName/MethodName/Arg1/Arg2

Also here is my .htaccess file:



        
相关标签:
2条回答
  • 2020-12-18 00:53

    You could try this variant that may work better:

    RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
    

    The changes that this makes are:

    1: using [\s\S] to match absolutely any character, instead of . which matches anything but a newline.

    Though you wouldn't normally expect newline (%0A) to be in your URLs, my suspicion is that Apache's regexp matcher is treating your input path as being in the ISO-8859-1 encoding.

    The IRI character U+0645 Arabic Letter Meem م UTF-8-URL-encodes to URI sequence %D9%85, and whilst byte 0xD9 is okay in ISO-8859-1, 0x85 decodes to U+0085 Next Line (NEL), an undesirable legacy control character that often counts as a newline. So if that happened, the expression .* wouldn't match it.

    Having said all that, this is quite theoretical as your example works as-is for me, on an old XAMPP 1.8.2 I had lying about on WinXP.

    2: using the [B] rewrite flag, to ensure all bytes are passed in correctly-URL-encoded form in the parameter.

    Otherwise, non-ASCII characters would break for situations where Apache sends the query string to PHP through Windows environment variables. The Windows environment is Unicode, so Apache has to decode the bytes on writing and PHP has to encode them again on reading, and unfortunately those encodings don't match.

    Apache uses ISO-8859-1 and PHP (via C stdlib) uses the ANSI code page, which depends on the locale of the Windows installation. On a Western install, you get code page 1252, which is close to ISO-8859-1 so only some of the bytes will be wrong (again, this includes the 0x85 in م); on other locales with other ANSI code pages all the non-ASCII characters will be wildly wrong.

    This doesn't necessarily apply to you as XAMPP is using mod_php, which doesn't need to use the environment to pass strings. But it would make a difference in other hosting environments. In any case, without [B] you'll find URL-special characters in the string (ampersand, plus, percent) break the query parser.

    0 讨论(0)
  • 2020-12-18 01:04

    Use URL encoder:

    String encodedURL = "www.example.com/ClassName/Methodname/254/" + URLEncoder.encode("Any text in any language", "utf-8");
    

    UTF-8 of course is the encoding to use.

    You can read more here: https://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html

    0 讨论(0)
提交回复
热议问题