开发者

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

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 va开发者_开发知识库riables 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜