1
RewriteRule ^(.*)_([^]+).htm/([0-9.]+)$ a.php?f=$1&t=$2&amt_from=$3 [NC]

I have form call a.php with 3 parameters. I am able to use the above rewrite rule, and a_b.htm/c does work.

My problem is when the user click a submit button on the form, they still see a.php?f=$1&t=$2&amt_from=$3

How do I fix that?

Thanks a lot.

2
  • Do you have any rewrite rules after the given one that match the requested URL? Commented May 10, 2011 at 20:10
  • Could you show the actual form or full contents of a.php? Commented May 10, 2011 at 20:30

2 Answers 2

1

You need your form to generate the correct URL. This is only doable with some Javascript, but basically works like this:

<form action="a.php" method="GET" onsubmit="rewrite_form(event);">
   <input name=f value=123>
   <input name=t value=ABCABC>
   <input name=amt_from value=XYZXYZXYZ>
   <input type=submit>

<script type="text/javascript">
   function rewrite_form(e) {    
      var form = document.forms[0];   // .getElementById("form1");
      window.location = '' + form.f.value + '_' + form.t.value + '.htm/' + form.amt_from.value;            
      if (e && e.preventDefault) { e.preventDefault(); }
      return false;
   }
</script>

So basically the form submit generates the nicer URL you want. If the browser doesn't support Javascript, it will fall back to the more elaborate GET-parameterized URL.

Le Update: The form element access was incorrect, you just need document.forms[0].fieldname.value usually. And I've moved the URL construction out into a separate function. The onsubmit= method works well enough with event.preventDefault(); in Opera/Chrome/Firefox.

Sign up to request clarification or add additional context in comments.

11 Comments

hey, mario, i am new to these. do u happen to have an example for my problem? what if i need to do some parse after i get $f/$t and using the parsed $f_p/$t_p in the url? thanks a lot
I don't know what you mean with parse. You already have the correct RewriteRule for the example JS url concatenation here. -- Or where/why do you want to parse it?
sorry, it's my mistake. all variables are good. i just working on them inside the php, but the variables are not changed. what u mean package with a callback. i am a js dummy. ty
With callback I just meant that you could move the location=...; assignment into a separate function. Then you could just use <form onsubmit="rewrite_locatation(this)" instead. But that's more for stylistic reasons, and wholly optional here unless you experience a conflict with the default submit method.
is this right? it doesn't work. sorry. really bad in js. <form name="cv" method="get" action="/sdir/fx.php" onsubmit="window.location.href=this.action + '/' + this.elements['f'].value+'_'+this.elements['t'].value+'.htm/'+this.elements['amt'].value;">
|
0

That is because you are still submitting with a method="get", so the form creates that form of url..

Use post instead if you dont want the user to see the submitted values in the url

4 Comments

thanks neal. i understand that, but i wish user see a_b.htm/c in the address bar. how to make that happen?
you really cant... unless from your php script you check the url and if its not what you want then do a header redirect
hmm, that should be a good solution. lots of people do that? i am new to php stuff
hmm, i don't know how to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.