Redirecting A Form Submission

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Redirecting A Form Submission

Post 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]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post 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]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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"
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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!
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post 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.
Post Reply