Page 1 of 1
Redirecting A Form Submission
Posted: Mon Feb 12, 2007 10:58 am
by seodevhead
Hey guys... I just implemented a mod_rewrite on an application, but am having trouble dealing with various html forms on my site that users can submit (simple drop down forms), that will take them to pages of their choice. These forms utilize the GET method of submission, and since I did mod_rewrite, I want it to be redirected to the static URLs.
How can I go about converting/directing the GET forms so that they send the user to the correct static rewritten URL?
Below is an example of my rewrite rule:
Code: Select all
RewriteRule ^([^/\.]+)/?$ page.php?w=$1 [L]
Posted: Mon Feb 12, 2007 11:43 am
by Kieran Huggins
remember: GET sends the info in the URL, so by mangling the URL you're effectively ruining the GET data. Can you use POST instead?
Posted: Mon Feb 12, 2007 12:16 pm
by seodevhead
Kieran Huggins wrote:remember: GET sends the info in the URL, so by mangling the URL you're effectively ruining the GET data. Can you use POST instead?
I can't use POST because then my mod_rewrite stuff wouldn't work. Even if I could get it working with POST... it would take me way too much time to reconfigure.
I am just attempting to find any URL's like so:
http://www.example.com/page.php?w=BB
..and do a 301 redirect to it's (mod_rewrite) rewritten URL sybling:
http://www.example.com/BB/
Here is what I have come up with so far, but it is created an endless loop:
Code: Select all
RewriteCond %{QUERY_STRING} ^w=([^&]+)$
RewriteRule ^page\.php$ http://www.example.com/?%1/? [R=301]
Posted: Mon Feb 12, 2007 12:42 pm
by Kieran Huggins
try:
Code: Select all
RewriteRule ^page\.php?w=([^&]+)$ http://www.example.com/%1/ [R=301]
?
But I still don't see how that will maintain GET form data.. unless all you care about is the value of "w"
Posted: Mon Feb 12, 2007 1:26 pm
by seodevhead
Hey Kieran,
I tried that without luck. I don't think you can place the GET variables in the first part of the rewriterule. It now seems that it is fairly impossible to do the following...
1) Use mod_rewrite to rewrite some URLs.
2) Create an HTML form (w/ method="GET") to send user to a URL with appended GET variables in the URL (like a search for instance ex. -> example.com/page.php?search="phpforums")
3) Redirect this dynamic URL to its associative "Rewrite" URL (ie. example.com/phpforums)
4) Allow Apache to then rewrite the above URL in #3 back to the original in #2.
If anyone knows differently or knows a solution, I am very interested. I have spent a long time doing a mod_rewrite but now am finding it very difficult to do such a seemingly easy task. It would be easy if I had simple <a href> links, but these forms on my site require processing in the rewritten URLs. Thanks for your help.
Posted: Mon Feb 12, 2007 10:31 pm
by Kieran Huggins
I'm fairly sure you can do it this way - and looking at the example I gave earlier I think I know what's wrong: I didn't escape the first '?' (it's just a regexp)
Try this (no RewriteCond):
Code: Select all
RewriteRule ^page\.php\?w=([^&]+)$ http://www.example.com/%1/ [R=301]
Maybe a more flexible solution would be (to catch the rest of the GET):
Code: Select all
RewriteRule ^page\.php\?w=([^&]+)&?(.+)?$ http://www.example.com/%1/?%2 [R=301]
Check out the new link in my sig (courtesy of pile0nades) - it's AWESOME! I'm like a kid in a candy store!
Posted: Tue Feb 13, 2007 6:23 pm
by seodevhead
Hey Kieran,
I don't know where the problem is, but I can't get it working either way. After much research, it just seems that an infinite loop will be created each time unless I use a marker to stop it (which is not ideal for the situation). I have made a workaround solution, however. I created a seperate php file to accept those form submissions and do a php 301 header redirect to the rewritten URL's... as opposed to doing a three-step redirect/rewrite with Apache alone. It works great, and I can get away with it since SE's don't submit forms.
Thanks so much for your help and assistance through this. It's nice to know there is a helping hand that is nice enough to lend it.