How to pass slash / in an URL?

Discussion in 'PHP' started by TheDataPlanet.com, May 18, 2011.

  1. #1
    On this page there are many abbreviation / acronym entries: http://abbreviations.wordcrow.com/acronyms/D/

    They are generated in PHP:

    <a href="/define/<?php echo rawurlencode($abbreviation['title'])?>/">
        <strong><?php echo $abbreviation['title']?></strong>
    </a>
    PHP:
    I used rawurlencode because many acronyms contain bizarre characters such as #, & and even /.

    And the URL requests such as:

    http://abbreviations.wordcrow.com/define/DA&E/

    Would first be fed to rawurldecode():

    $acronym = rawurldecode('DA%26E'); // $acronym would be 'DA&E'.
    PHP:
    And then used in database queries.

    While DA&E is all right, DA/C is not. Try this URL http://abbreviations.wordcrow.com/define/DA/C/ and you would end up with http://abbreviations.wordcrow.com/define/DA/C/ which is an error page.

    I can extend more code to recognize /define/DA/C/ but it's just weird and non-sensible. I tried both Chrome and Firefox and they all automatically convert DA%2FC to DA/C. But with DA%26E, they don't.

    What am I doing wrong? Ain't that / already encoded into '%2F'?? Really weird. Any help would be appreciated!
     
    TheDataPlanet.com, May 18, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Do you have any automatic output escaping or something similar. I've seen this when there is something escaping data and the url string gets double escaped.
     
    jestep, May 18, 2011 IP
  3. TheDataPlanet.com

    TheDataPlanet.com Well-Known Member

    Messages:
    503
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Thanks, TwineDev. Here's the .htaccess in the /define/ directory:

    <IfModule mod_rewrite.c>
    
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    
    </IfModule>
    Code (markup):
    And in /define/index.php:

    $req = explode('/', $_SERVER['REQUEST_URI']);
    
    $acronym = rawurldecode($req[2]);
    
    ... // Database queries with $acronym
    Code (markup):
    I tried to add "AllowEncodedSlashes On" in the .htaccess but it didn't work. Any idea?
     
    TheDataPlanet.com, May 18, 2011 IP