1

As the topic - How do I rewrite a POST request from a form to a user-friendly URL with htaccess?

The scenario:

I have a webpage that uses a search-form. When I submit that form using method="post" it works flawlessly. BUT I don't get any text in the browser address-bar (of course), but that's exactly what I want! And that by using method="POST", NOT method="GET"!

Let's say I search for "banana". The PHP script translates the POST-request and the script shows all receipts with the word banana in it. But the URL then of course shows something like http://www.example.com/search/ (yes I use mod_rewrite for that). I want the URL to look like http://www.example.com/search/banana/.

The original request from the server looks like ....xample.com/index.php?p=search and the post is of course hidden and would otherwise be ....xample.com/index.php?p=search&q=banana.

I'm not new to mod_rewrite rules and conditions but I just can't get it to work...

Thanks in advance!

Widerberg
  • 1,118
  • 1
  • 10
  • 24

3 Answers3

3

Quoted from: Apache mod_rewrite question

You can't use POST data for mod_rewrite. This is because the POST data isn't in the HEADER of the http request, it's in the BODY.

My suggestion would be that you perform an action on the posting page that adds the prefix to the URL, which would mean you don't even need to rewrite.

Community
  • 1
  • 1
JoshStrange
  • 1,121
  • 1
  • 7
  • 22
0

If I understand correctly: you want the browser to POST to http://www.example.com/search/banana/ rather than to http://www.example.com/search/, where banana is one of the form's input-fields. (Is that right?) That's not really a mod_rewrite issue, then, so much as an HTML issue: it happens on the client side. And since HTML doesn't support this, it's actually a JavaScript issue. You'd have to write either an on-submit-handler for your form, or an on-change-handler for the input field. In either case, the handler will have to modify the form's action based on the contents of the input-field.

(Note: The above is also true, to a certain extent, for GET requests. The major difference is that with a GET request, you could circumvent this by HTTP-redirecting from http://www.example.com/search/?q=banana to http://www.example.com/search/banana/; so the browser will initially GET http://www.example.com/search/?q=banana, and then GET http://www.example.com/search/banana/. But according to the HTTP spec, you can't redirect a POST request in the same way.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Nah, not really... Here "banana" stands for the query generated in turn of the (x)html form. Thanks for the reply though =) the forms name is "q". – Widerberg Dec 20 '11 at 20:15
  • @AlexanderWiderberg: I think we're saying the same thing. (When I wrote "`banana` is one of the form's input-fields", I should really have written, "`banana` is **the value of** one of the form's input-fields". Or, if you prefer, "`banana` is **what the user typed into** one of the form's input-fields.") – ruakh Dec 20 '11 at 20:29
0

You will need javascript.

I answered a similar question before. When someone click on the submit button you will need to change the action attribute at the form tag.

Community
  • 1
  • 1
Sawny
  • 1,404
  • 2
  • 14
  • 31