Link to home
Start Free TrialLog in
Avatar of NovoWeb
NovoWebFlag for Canada

asked on

UrlEncode and apostrophes

Hi,

I use the UrlEncode in my .htAccess files with PHP and MySql.

EX:
$Data .= 'RewriteRule '.formatURL_htaccess(regionId2Name_htaccess($rRow["id"])).'/'.formatURL_htaccess($vRow["ville"]).'/$ /villes.php?region='.$rRow["id"].'&ville='.urlencode($vRow["ville"]).' [L]
';

The variable $_GET['ville'] is UrlEncode. This variable content cities like "Ayer's Cliff". The cities that have apostrophes in it are showing like this "Ayer7s Cliff" in the target page. The UrlEncode is encoding the " ' " with %27 and that is normal but why in the target page it's show the " 7 " ?

All other code are ok.

Thanks!
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I have never seen anyone do this before.  Where did you get the idea?  What do you expect it to be doing for you that is different from regular methods of access?
Avatar of Dave Baldwin
It's looks like you are encoding it twice and decoding once.  I'm not sure why you are doing it in '.htaccess'.  A normal HTTP GET or POST is urlencoded in the browser before it is sent.
Avatar of NovoWeb

ASKER

Hi,

I do this do have clean URL's. Here is the result in the .htaccess file:

RewriteRule cantons-de-l-est/ayer-s-cliff/$ /villes.php?region=6&ville=Ayer%27s%20Cliff [L]

I understand but if you urlencode something that has already been encoded, it just messes it up.  Like I said, A normal HTTP GET or POST is urlencoded in the browser before it is sent.  You don't have to re-encode the querystring.
Avatar of NovoWeb

ASKER

Yes i understand, but if i don't encode it, since there are space or apostrophes in the cities it gave me a error server of 500.
Then something else is going on because I don't have that problem on my many PHP pages.  In addition, all the code you have shown above is PHP code, not '.htaccess' code so I don't know how it is getting into '.htaccess'.  I searched Google for 'formatURL_htaccess' and this question is the only place it shows up.  Where did you get it?  Is there a framework or CMS that this is a part of?
Avatar of NovoWeb

ASKER

The  'formatURL_htaccess' is a simple function that i created to replace é to e , à to a etc... and replace all sapces with '-' and remove all special charcarters with '-'. That's all!

All these codes are in a *.php file and it is writing the .htaccess file with the "fopen($xml, "w")" , fwrite($Handle, $Data) and fclose($Handle) functions.
Do you realize that your function runs AFTER the .htaccess file is read by Apache?  The file that you write will be used for the NEXT page requested, not the current one.
Avatar of NovoWeb

ASKER

No. The file is only running when they are update... about once a week, not on every load of the index file !!! I am maybe weird but not crazy :)
Since you wrote that code and you're the only one that knows about it, I don't know how I can help you.  Good luck.
instead of:
urlencode(...)

try the base64 encoding function given below:
base64_url_encode(...)

This means that you will also need the "sibling" decoding function base64_url_decode(...)



<?php
function base64_url_encode($input) {
    return strtr(base64_encode($input), '+/=', '-_,');
    }

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_,', '+/='));
    }


$Data .= 'RewriteRule '.formatURL_htaccess(regionId2Name_htaccess($rRow["id"])).'/'.formatURL_htaccess($vRow["ville"]).'/$ /villes.php?region='.$rRow["id"].'&ville='.base64_url_encode($vRow["ville"]).' [L]
';
?>

Open in new window

actually, use these:

function base64_url_encode($input) {
    return strtr(base64_encode($input), '+/=', '-_~');
    }

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_~', '+/='));
    }

Open in new window

i dont think php will work in .htaccess. atleast i didnt try it.

rather i do a forward to a php file which does the matching and calculation and do a permanent 403 redirect from there
I've read this question over a couple of times and I still do not get it.  Can you please post an example of the input you've got and show us the output you want?  Maybe I am missing something, but I think you're writing programming to do something that already happens automatically in the PHP environment.

See the code snippet.  It contains a string that we urlencode() for HTTP transport.  We send the string to a script that will just echo back what is in the URL argument.  Because the echo script has magic_quotes set on, we will see that the apostrophe is escaped.  
http://www.laprbass.com/RAY_temp_novoweb.php

Script output is this:
string(12) "Ayer's Cliff"
string(14) "Ayer%27s+Cliff"
<pre>
array(1) {
  ["q"]=>
  string(13) "Ayer\'s Cliff"
}
 
<?php // RAY_temp_novoweb.php
error_reporting(E_ALL);
echo "<pre>";

// A TEST DATA STRING
$str = "Ayer's Cliff";
var_dump($str);

// URL-ENCODED FOR HTTP TRANSPORT
$arg = URLEncode($str);
var_dump($arg);

// SEND THE ARGUMENT TO A SCRIPT THAT WILL ECHO THE STRING
$url = "http://www.laprbass.com/RAY_bounce_get.php?q=$arg";
echo htmlentities(file_get_contents($url));

Open in new window

The echo script is the essence of simplicity.
<?php // RAY_bounce_get.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;
var_dump($_GET);

Open in new window

As far as translating things like é to e , à to a -- well, I would want to do that after the data has been received in my PHP script.  These character set translations are often confusing.  Maybe this article can help clear some of it up.
http://www.joelonsoftware.com/articles/Unicode.html
ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NovoWeb

ASKER

THanks all!

Finally a got it running...

All url are clean

To see it in action, go to http://www.chaletslocationsvacances.com

It's a french site but you see what i mean.

Thanks again and have a good day!