Jump to content
Larry Ullman's Book Forums

Ch03 Mod Rewrite And Search Query String


Recommended Posts

I have a question about the mod rewrite section in Ch 03. After creating the .htaccess file, the search form stops working. Even after changing the action to /search/, it no longer recognises the additional query string (i.e. &terms=) and so you always receive the 'Please use the search form to search this site.' notice. If I change the form method to POST and change the $_GETs to $_POSTs in search.inc.php for processing, it works fine.

 

I'm assuming that it has to do with the mod rewrite in that it rewrites /search/ to index.php?p=search but the rest of the query string supplied by the form is lost.

 

(I checked $_GET upon arrival in search.inc.php and the only value is for p even though terms=whatever shows in the URL: /search/?terms=whatever)

 

Can someone tell me how to adjust the mod rewrite so that if there are additions made to the query string via search form, e.g., it will recognise these? (ie if the URL says /search/?terms=whatever it will rewrite this to index.php?p=search&terms=whatever but if it says /about/ it will work as described in the book)?

 

Or if I'm off the mark and it's NOT the mod rewrite, then what else about the search form and processing needs to change?

 

Thanks.

Link to comment
Share on other sites

kmichael, welcome to the forums.

 

If I understand you correctly, you're using mod_rewrite to make prettier URLs, but in the process, you're losing the query parameter part of the URL, thus making it impossible to get information via the $_GET superglobal in PHP, yes?

 

Assuming, that's the case, instead of using $_GET, you may want to parse the URL string itself, and then do what needs to be done from there.

You can use the $_SERVER superglobal in combination with the parse_url  function (http://php.net/manual/en/function.parse-url.php) to get the current URL and parse it as need be.

 

That's just one possibility, of course.

There may be better ways, depending on what exactly you want.

Link to comment
Share on other sites

Thanks for the suggestion and you did understand the problem correctly. I suppose I was looking to see if there was a solution that involved the mod_rewrite itself, such as do you need to now write a new rule to cover the search form alone (and what this would look like). The whole issue came about as I was working through Chapter 3. Once I created the .htaccess (to create the pretty urls), the previous work for processing a search form stopped working but this was never addressed in the chapter. It tells you to change the 'action' reference in the search form, but it doesn't tell you that you will lose your query string or what to do about it.

Link to comment
Share on other sites

  • 1 year later...
 Share

×
×
  • Create New...