dj-kenpo Posted July 20, 2007 Share Posted July 20, 2007 is it possible to control the URL sent from a web form of the GET type? write now I have my little search form, user types word(s) hits search, but instead of "http://domain.com/search.php?words=word1+word2" I'd like to take advantage of my htaccess and rewrite to search/word1+word2. I can do this easy in htaccess, but how do i get the FORM to send the url THIS way? anyone with experience in controlling form urls? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/ Share on other sites More sharing options...
Daniel0 Posted July 20, 2007 Share Posted July 20, 2007 Hmm... you could use Javascript... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Search</title> <script type='text/javascript'> function search() { location.href = 'search/'+document.getElementById('search_query').value.replace(' ','+'); } </script> </head> <body> <h1>Search</h1> <form action='search.php' onsubmit='search(); return false;'> <label for='query'>Search query:</label> <input type='text' name='words' id='search_query' /> <button type='submit'>Search</button> </form> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303535 Share on other sites More sharing options...
corbin Posted July 20, 2007 Share Posted July 20, 2007 You could use htaccess to silently forward it.... You would have a lot of htaccess going on though haha.... Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303543 Share on other sites More sharing options...
dj-kenpo Posted July 20, 2007 Author Share Posted July 20, 2007 I'm already using htaccess, as I said, (that portion is fine) the problem is the get from not wanting to format the search string in any way but the default.. ugly way... @Daniel the javascript method is really smart, but I'd like to not corner myself is a user has javascript disabled... Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303561 Share on other sites More sharing options...
corbin Posted July 20, 2007 Share Posted July 20, 2007 No I meant use it for the form too.... For example: you submit form and it goes to ?words=blah, but htaccess rewrites it to /search/blah.... The only problem would be, you would have to be careful with when htaccess rewrote it ... again... so that it wouldn't get in a loop. Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303579 Share on other sites More sharing options...
dj-kenpo Posted July 20, 2007 Author Share Posted July 20, 2007 htaccess can't rewrite it that way, as the form is sending the url data to the browser... it's like asking htaccess to find links on a page and rewrite them, doesn't work that way, but thanks for the thought anyways Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303605 Share on other sites More sharing options...
corbin Posted July 20, 2007 Share Posted July 20, 2007 But the url data being sent to the browser turns into a request to the server, which htaccess can rewrite. Edit: the first mod, taking the form data to another url, would be more of redirection, but it would have to involve rewrite.... Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303614 Share on other sites More sharing options...
dj-kenpo Posted July 20, 2007 Author Share Posted July 20, 2007 I don't really get what you're saying, if you think it would work though and could back it with some sample htaccess code I'm all for trying it. what you're saying is a redirect and then a rewrite? if I understand at all? I still don't see how it would work, but form gets are rather different so i wouldn't be too surprissed if it worked somehow and I just overlooked something very basic... Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303634 Share on other sites More sharing options...
GingerRobot Posted July 20, 2007 Share Posted July 20, 2007 Hmm, i could be way off - never used any sort of rewriting at all, but could you not have some intermediate php script which all your forms point to? So you submit your form to say, rewrite.php which takes all the variables from the form, rewrites them into the url you wanted, and redirects to that? Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303642 Share on other sites More sharing options...
Daniel0 Posted July 20, 2007 Share Posted July 20, 2007 @Daniel the javascript method is really smart, but I'd like to not corner myself is a user has javascript disabled... In the script I provided, if the user doesn't have Javascript enabled, then it'll use a regular submit. If you rewrite /search/whatever to search.php?words=whatever then it'll work. Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303719 Share on other sites More sharing options...
dj-kenpo Posted July 20, 2007 Author Share Posted July 20, 2007 @Daniel oh man, do I ever feel like a noob.... I'm no js guy so i didn't know it would still submit, the code works perfect! Thanks! (I might be a little anal or paranoid, but still... I'd like to hide my structure as much as possible......) Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303731 Share on other sites More sharing options...
corbin Posted July 20, 2007 Share Posted July 20, 2007 The Javascript method works fine, but just incase.... The htaccess would look something like this: RewriteEngine on RewriteRule ^search\.php\?search=(.+)$ /search/$1/ [R] RewriteRule ^search/(.+)$ process_search.php?search=$1 I haven't tested that, and I'm terrible at mod_rewrite stuff, but hopefully you get the idea.... You would use it something like: <form action="search.php" method="GET"> <input type="text" name="search" /><br /> <input type="submit" value="Search!" /> </form> When you submitted this form it would go to search.php?search=<what ever was in the box>. Mod_rewrite would rewrite that in the address bar to search/<what ever was in the box>.... Mod rewrite would then, behind the scenes, rewrite that to process_search.php?search=<what ever was in the box>. It's quite messy, and I would be surprised if what I posted actually works (like, seriously... I'm baaaddd at mod_rewrite stuff), but it could be something to consider. (P.S. I get what you were saying when you said that mod_rewrite doesn't work like that.... If it doesn't make a new request when the page was written in the second step, then it would stop and this would be pointless, but I'm fairly sure it makes a new request.) Quote Link to comment https://forums.phpfreaks.com/topic/61000-solved-url-rewrite-on-a-get-form/#findComment-303759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.