Page 1 of 1

urlencode question

Posted: Wed Mar 11, 2009 1:58 pm
by paqman
I'm in the last stages of a site but I'm having one annoying problem. I'm trying to create urls for a news page which look a little nicer, using the title as part of the url (versus an id tag). I'm trying to use urlencode() to get the title in the right format for being used as part of the url, however, it does not encode non-alphanumeric characters, like a question mark. So if the title is 'Click Here?' url encode makes it 'Click+Here?'. When clicking on this link, the browser automatically converts the '?' to %3F. Using $_GET however, this %3F is lost, which then means it can't find the entry.

I'm also using mod rewrite in a .htaccess file, which I suspect may be another possible culprit. Here's the line that is handling this particular request (my first shot at using mod rewrite):

Code: Select all

 
RewriteRule ^(academic|drama|green|music|newspaper|newsletter|sports|studentcouncil|youthministry|parents|alumni)/([^/\.]+)/(.*) /test/department.php?department=$1&$2=$3
 
It seems to work fine so long as a character isn't an 'irregular' one (like ?, #, etc).

If you need to check it out, the site is located at http://stmc.bc.ca/test/. The 'Current' is the problem (try the top entry). The page outputs the $_GET["news"] (where news is $2 in the .htaccess) at the top...

Thanks!

Re: urlencode question

Posted: Wed Mar 11, 2009 2:16 pm
by requinix
Relying on the title being typed correctly is risky, especially since, like, everything out there does it differently. People are human: if they type "commonly-mispelled-words" instead of "commonly-misspelled-words" your system won't know about it.

This place is a little behind on the times, but the "normal" scheme is to have a URL along the lines of

Code: Select all

http://forums.devnetwork.net/php-code-1/urlencode-question-96685
The name gets converted into something close to the original but still URL safe, then the internal ID number gets appended. The RewriteRule for a thread would look like

Code: Select all

RewriteRule ^/?[^/]+-(\d+)/.*-(\d+) viewtopic.php?f=$1&t=$2
By using an ID people know (assume) it's important and are free to make as many typos in the fake thread title as they wish.


To address your original concern, though, the function you want is rawurlencode.

Re: urlencode question

Posted: Wed Mar 11, 2009 3:05 pm
by paqman
Turns out firefox was turning rawurlencode (when inside a hyperlink href) back into its previous value... (IE wasn't doing this). Very annoying. So to simplify everything I just decided to use preg_match in the admin area to only allow alphanumeric characters for titles for those entries. Thanks for the help though.