views:

1041

answers:

3

So I'm building a simple php application using CodeIgniter that is similar to Let Me Google That For You where you write a sentence into a text input box, click submit, and you are taken to a URL that displays the result. I wanted the URL to be human-editable and relatively simple so I've gotten around the CodeIgniter URL routing a bit. So right now my URLs can look something like this:

http://website.com/?q=this+is+a+normal+url

My problem right now is when the sentence contains a special character like a question mark or a backslash. Both of these mess up with my current .htaccess rewrite rules and it happens even when the character is encoded.

http://website.com/?q=this+is+a+normal+url? OR http://website.com/?q=this+is+a+normal+url%3F

What does work is double-encoding, for example if I take the question mark and encode it to %253F (where the ? is encoded to %3F and the % sign is encoded to %25). So this url works properly.

http://website.com/?q=this+is+a+normal+url%253F

Any idea what I can do here? Is there a clever way I could double encode the input? Can I write a .htaccess rewrite rule to get around this? I'm a little at a loss here.

Here are the rewrite rules I'm currently using for everyone.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^(.*)$ /index.php/app/create/%{QUERY_STRING}? [L]

Note that the way CodeIgniter works is they have a index/application/function/parameter URL setup. I'm feeding the function the full query string right now.

A: 

I usually do human readable urls like this

$humanReadableUrl= implode("_",preg_split('/\W+/', trim($input), -1, PREG_SPLIT_NO_EMPTY));

It will remove any non-word characters and will add underscores beetween words

waney
Unfortunately things like punctuation are necessary for the app (the users are going to want to put in exclamations, periods, and question marks).
drinkycode
+1  A: 

If your’re using Apache 2.2 and later, you can use the B flag to force the backreference to be escaped:

RewriteCond %{QUERY_STRING} ^q=.*
RewriteRule ^ /index.php/app/create/%0? [L,B]
Gumbo
Thank you! I changed the .htaccess file to use what you had and it worked exactly as I needed. I would vote you reputation points if I had any to give.
drinkycode
A: 

I think what you actually want is this:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^(.*)$ /index.php/app/create/ [L,QSA]

QSA appends the existing query string to the new URL

Greg