Jump to content

[SOLVED] url rewrite on a GET form????


dj-kenpo

Recommended Posts

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

@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......)

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.