...

View Full Version : How to pass slash / in an URL?



immediate
05-18-2011, 12:33 PM
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>

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():


$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!

abduraooft
05-18-2011, 01:23 PM
How does your rewrite rule look like?

If you use a rule like
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.

immediate
05-18-2011, 05:15 PM
How does your rewrite rule look like?

If you use a rule like
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:


<IfModule mod_rewrite.c>

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

</IfModule>

And in /define/index.php:


$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?

Horologe
05-18-2011, 05:33 PM
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.

abduraooft
05-18-2011, 06:34 PM
$acronym = rawurldecode($req[2]);

You can't just take $req[2] only in this case.

How about
// 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?

immediate
05-19-2011, 03:32 AM
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.

immediate
05-19-2011, 03:35 AM
http://www.w3schools.com/tags/ref_urlencode.asp

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum