Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2010
    Location
    kavoir.com
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to pass slash / in an URL?

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

    They are generated in PHP:

    PHP Code:
    <a href="/define/<?php echo rawurlencode($abbreviation['title'])?>/">
        <strong><?php echo $abbreviation['title']?></strong>
    </a>
    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%26E/

    Would first be fed to rawurldecode():

    PHP Code:
    $acronym rawurldecode('DA%26E'); // $acronym would be 'DA&E'. 
    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%2FC/ 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!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,856
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    How does your rewrite rule look like?

    If you use a rule like
    Code:
    RewriteRule ^define/(.*)$ index.php [L]
    , it will redirect all the requests containing define/ to index.php, and then you could use $_SERVER['REQUEST_URI'] to get the required string.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    May 2010
    Location
    kavoir.com
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    How does your rewrite rule look like?

    If you use a rule like
    Code:
    RewriteRule ^define/(.*)$ index.php [L]
    , it will redirect all the requests containing define/ to index.php, and then you could use $_SERVER['REQUEST_URI'] to get the required string.
    Thanks, abduraooft. You are right. I'm using just the thing.

    Here's the .htaccess in the /define/ directory:

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

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

  • #4
    New Coder
    Join Date
    May 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So basically you're trying to get around URLEntities?http://www.w3schools.com/tags/ref_urlencode.asp

    A solution would be to 2-way encrypt the data you want to pass... No more URL entities in urls. no more weird behavior.
    Last edited by Horologe; 05-18-2011 at 04:35 PM. Reason: added information

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,856
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    $acronym = rawurldecode($req[2]);
    You can't just take $req[2] only in this case.

    How about
    PHP Code:
    // say $_SERVER['REQUEST_URI'] = '/define/DA/C/';
    echo $req=trim($_SERVER['REQUEST_URI'],'/');
    echo 
    '<br>'.$req=str_replace('define/','',$req); 
    and then use $req in the query?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    New Coder
    Join Date
    May 2010
    Location
    kavoir.com
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    and then use $req in the query?
    The error is at Apache level, not PHP level - I can't even get to PHP level when you click 'DC/A' at http://abbreviations.wordcrow.com/acronyms/D/ because it's a blank page, not 'DC'.

    A solution would be to 2-way encrypt the data you want to pass... No more URL entities in urls. no more weird behavior.
    May I known what a 2-way encrypt is? Do you mean slugs? I know slugs would definitely get this to work, but I really want to do without them.

  • #7
    New Coder
    Join Date
    May 2010
    Location
    kavoir.com
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is very helpful, simple and straightforward. Thanks! But I still don't understand why Apache would give me a blank page when I use %2F in the URL.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •