Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a website with a search box on it. The search term is passed to the search page as part of the $_GET (I'm using PHP, but I think that's irrelevant to the problem I'm having), so my final url would be something like www.example.com/search.php?searchterm=smith. However, I'm finding that if an apostrophe is included in the search term (e.g. www.example.com/search.php?searchterm=o'hagen) the server is returning a 406 error. I thought I may be able to catch this in the htaccess file and rewrite it something usable but I haven't been able to so far. I've tried these so far:

RewriteRule ^([^/]*)'([^/]*)$ $1\'$2 [N,E=redirect:1]

and

RewriteRule ^([^/]*)'([^/]*)$ $1%27$2 [N,E=redirect:1]

Neither of these did me any good. Can anyone suggest what I can use instead?

EDIT: For the moment I've gone with changing the search form to POST and handling this in code but I'm still curious as to how I can get apache to accept the search term in a GET query string.

share|improve this question
1  
With php you can(could) urlencode() strings.. maybe that's what you're looking for? –  NickW May 13 '13 at 16:09
    
@NickW - The string never hits PHP so it can't be urlencoded there. I tried encoding it in javascript before sending it, but that made no difference. –  hellsgate May 13 '13 at 16:14
add comment

2 Answers

Perhaps what you're getting in the URL isn't an apostrophe, but a curly single quote.. which evades your regex?

share|improve this answer
    
Thanks, it's a good thought. However, while I've been testing it I've been typing the apostrophe myself so it definitely isn't a curly single quote. –  hellsgate May 14 '13 at 7:58
add comment

Why not just use POST instead? If you really need the search term in your URL, you can POST it to an intermediate script, urlencode it, then redirect the browser basically transparently:

Form posts to "s.php":

<?php if (isset($_POST['search'])) { $s = urlencode($_POST['search']); header("Location: /search.php?searchterm=$s"); ?>

Redirect goes to your search.php script with a "safe" encoded string.

share|improve this answer
    
I decided to take the half-way solution from this: instead of putting the search term in the query string I just changed the search form to post. So I now have a working search form –  hellsgate May 14 '13 at 8:18
    
You could also url encode the search string with javascript before it's sent to the server. –  ultraviol3tlux May 14 '13 at 15:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.