Apache: mod_rewrite: Spcaes & Special Characters in URL not working

假装没事ソ 提交于 2020-01-16 07:10:40

问题


i am using APACHE:mod_rewrite to define a set of rules for rewritting URLS

i want this link                                                           to be displayed as

/myDIR/walls.php?f=All&of=0&s=Newest -> All.html

so i am using the following rule

text from (.htaccess)

RewriteEngine  on
RewriteBase    /myDIR/   
RewriteRule ^All\.html$  papers.php?f=All&of=0&s=Newest

now these variables that are being passed as f=All of=0 s=Newest these are being used in query, OBVIOUSLY, and one of these variables, i.e f sometimes has values with spaces and special characters, and i can't avoid that because the database is already in-place and all i am doing rewrite of URLs....

NOW when i try to define a rule like this

i want this link                                                                                          to be displayed as

/myDIR/walls.php?f=Characters & Supers&of=0&s=Newest -> Characters & Supers.html

which is wrong i know because there shouldn't be any spaces.. so to make it right i define the rule like this

RewriteRule ^Characters%20%26%20Supers\.html$  papers.php?f=Characters%20%26%20Supers&of=0&s=Newest

it lets me define the rule but when i click my link i get this 404 Not Found Error "The requested URL /wallz/Characters & Supers.html was not found on this server."

QUESTION: WHAT To Do ?

my guess is i am not supposed to be doing HTML URL Encoding inside .htaccess


回答1:


Your guess is correct. By the time that a URL has reached mod_rewrite for processing, Apache has already decoded the URL for you. Therefore, if you want to check for any otherwise-encoded characters, you need to use their literal representations.

Since whitespace is used as a delimiter here, you'd also need to escape your spaces:

RewriteRule ^Characters\ &\ Supers\.html$ papers.php?f=%0&of=0&s=Newest

I believe that using quotation marks will also work, so the following should be equivalent (though it's untested):

RewriteRule "^Characters & Supers\.html$" papers.php?f=%0&of=0&s=Newest

A final note about Apache's decoding process is that it will automatically remove multiple slashes for you. While not relevant for your example, it's another example of how the URL is transformed prior to reaching your mod_rewrite rules.



来源:https://stackoverflow.com/questions/4215673/apache-mod-rewrite-spcaes-special-characters-in-url-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!