FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


.htaccess issues with & and ? characters in title





Aredon
This is my current HTaccess setup:

Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /food-and-diet/
RewriteRule ^.+(/css/.+)$ $1 [L]
RewriteRule ^.+(/js/.+)$ $1 [L]
RewriteRule ^.+(/images/.+)$ $1 [L]
RewriteRule ^\index.html$ index.php [NC,L]
Rewriterule ^articles/?$ index.php?index=$1&article=none&folder=none [NC,L]
Rewriterule ^articles/([^.^/]+)/?$ index.php?article=none&folder=$1 [NC,L]
RewriteRule ^articles/([^/]+)\.jpg$ articles/$1.jpg [L]
RewriteRule ^articles/([^/]+)\.gif$ articles/$1.gif [L]
RewriteRule ^articles/([^/]+)\.png$ articles/$1.png [L]
Rewriterule ^articles/([^/]+)\.html$ index.php?article=$1&folder=none [NC,L]
RewriteRule ^articles/([^.^/]+)/([^/]+)\.jpg$ articles/$1/$2.jpg [L]
RewriteRule ^articles/([^.^/]+)/([^/]+)\.gif$ articles/$1/$2.gif [L]
RewriteRule ^articles/([^.^/]+)/([^/]+)\.png$ articles/$1/$2.png [L]
Rewriterule ^articles/([^.^/]+)/([^/]+)\.html$ index.php?article=$2&folder=$1 [NC,L]


I need a way to make both of these urls work without breaking any $_GET statements.
Code:
/food-and-diet/articles/test-&-cake.html
and
Code:
/food-and-diet/articles/testycake?.html

Basically I need & to be ignored when ? isn't present, and vice versa. I'm tired of struggling with it, pwease halp!
Aredon
Update: Looks like even if I urlencode the links and have it as %26 the .htaccess seems to be decoding the URL and i get separated $_GET variables.
Code:
print_r($_GET) = Array ( [article] => test- [-cake] => [folder] => none )
jmraker
If the contents of the string $_SERVER['QUERY_STRING'] is good enough you can parse the folder parameter yourself with a regex function like
Code:
if(preg_match('/&folder=(.*?)$/', $_SERVER['QUERY_STRING'], $arr))
    $_GET['folder'] = $arr[1];


$_SERVER['QUERY_STRING'] variable contains the original parameters before PHP parsed them into the $_GET array, so it should be possible to get the folder if it contains an &

If I had to do this I would add a special "the program was called by an .htaccess rule by adding a &htaccess=Y&folder=abc&def

so if a future developer comes along and makes a link to index.php?folder=$1&article=$2 they won't have to figure out why &article=abc is working but it's also part of the folder name

Rewriterule ^articles/([^.^/]+)/([^/]+)\.html$ index.php?htaccess=Y&article=$2&folder=$1 [NC,L]

If you want to remove the ? and & letters from the folder you could do
$folderName = str_replace(array('?', '&'), array('_', '_'), $folderName); before it's written to the database
Fire Boar
I'd definitely suggest delegating the URL parsing to a PHP script rather than .htaccess - usually the most you'll need is

Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


That means "if file/directory does not exist, and the request is not for the file favicon.ico, redirect to index.php with $_GET['q'] as the path requested.

Extra point: Be careful about ? and &. I'd strongly recommending removing those characters from your URLs, but if you insist on using them you'll need to handle those in PHP by parsing the $_GET array and figuring out what goes where.
Aredon
I actually just worked around it and removed ? from allowed characters. Apparently it creates invalid file names that then can't be opened anyway. As far as & goes, I ended up making sure everything was url_encoded before being submitted, so it works fine now. I'll definitely have to play with using php for url rewrite instead of .htaccess. That's easily some of the most frustrating lines of code I've ever worked with.
Related topics
Master Tutorial on SEO
Poor customer service: Name & Shame
CUSTOM ERROR PAGES
The antichrist
Syndicate Rich Media
Indie Music
Red Alert 2
Conan Dectective
Fallout 3
Honourable Chief Justice, You're Fired!
Free Web Hosting for JSP
htaccess Problem
How do other sites do what I want to do?
ClipArt 500,000 Images ///2,12 Gb total
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.