Solution to 404 error returned when accessing the encoded Chinese URL

Source: Internet
Author: User
This article mainly introduces how to solve the 404 error returned by the Chinese URL after the access code. This article uses the replacement method, of course, you can also use the encryption method to solve the problem, you can refer to a project you created yesterday. one requirement is that each image corresponds to a short text description of the image, A common practice is to create a new table and record the image name and description in the database. After careful consideration, I feel that this application can be completed without a database. the solution I implemented is to use URLENCODE as the file name, in this way, when I read the file, I can use the file name URLDECODE to describe the text of the image after the drive.

However, when accessing images through a browser, the system prompts that the file cannot be found. for example, the description text of an image is "Qiongtai Blog". the file name generated after URLENCODE is as follows:

The code is as follows:


%E7%90%BC%E5%8F%B0%E5%8D%9A%E5% AE %A2.jpg

So I accessed the image through a browser and prompted that the image could not be found.

After careful reading, I found that the file name was automatically transferred back to Chinese when the browser accessed it.

Firefox

Chrome

IE

Safari

IE and Safari do not see the conversion from the address bar to Chinese characters, but they also prompt that the file cannot be found. But I think it should have been automatically converted during the request, but the address bar does not show the converted ones. View the requests for accessing images from Nginx access records

The code is as follows:


192.168.6.30--[12/Oct/2012: 10: 09: 44 + 0800] "GET/clusters HTTP/1.1" 404 199 "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0 )"

No exception was found in request URL processing. finally, after studying the encoded file name repeatedly, it was found that all the results were composed of a percent sign and a letter number, I think the browser may perform some other conversions when it encounters a percent sign, so the prompt that the file after the browser accesses URLENCODE cannot be found.

So I replaced all the percentage signs in the file names after URLENCODE with underscores.

The code is as follows:


%E7%90%BC%E5%8F%B0%E5%8D%9A%E5% AE %A2.jpg

Replace

The code is as follows:


_E7_90_BC_E5_8F_B0_E5_8D_9A_E5_ AE _A2.jpg

Solve the problem by re-accessing the browser

To obtain the text description of an image, replace "_" in the file name with "%" and then use URLDECODE.

Note that the file name in Linux has the same length limit as that in Windows. Currently, the most common format is ext3, which can contain 255 characters, after about five characters are deducted as extended names, there are about 250 pure-length file names remaining, and the length of one Chinese character after URLENCODE is 9. Therefore, a maximum of 27 Chinese characters can be encoded as file names..

Although this method stores fewer Chinese characters, you can use some encryption methods to obtain a short string of ciphertext, and then URLENCODE the ciphertext. I will not give an example of the specific implementation method, start thinking about it!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.