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!
NovoWebAsked:
Who is Participating?
 
skullnobrainsCommented:
rawurlencode() should work out of the box but i would no recommend using it

http_build_query() definitely will work fine in your case
something like this

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

like previously stated, i also think it a little weird to make use of htaccess files to accomplish such a goal, but i guess you have good reasons. maybe with a little more information...

good luck with the coding
0
 
Ray PaseurCommented:
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?
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
NovoWebAuthor Commented:
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]

0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
NovoWebAuthor Commented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
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?
0
 
NovoWebAuthor Commented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
NovoWebAuthor Commented:
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 :)
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
hieloCommented:
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

0
 
hieloCommented:
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

0
 
liveaspankajCommented:
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
0
 
Ray PaseurCommented:
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
0
 
NovoWebAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.