Jump to content

How To Rewrite A Form Submitted URL


jimmyoneshot

Recommended Posts

I've managed to rewrite most of my basic url's using rewrite rules in my htaccess file however I have a search function in my page which is applied using a form. My homepage url is this:-

 

http://www.mysite.com/mysite/mainpage/

 

And when the search button is clicked whatever the user has typed in will be sent back to the page and the page will be filtered based on this.

 

I want the url that is displayed after the search form is activated to be:-

 

http://www.mysite.com/mysite/mainpage/Search/SearchTerm=blah

 

With 'blah' of course being whatever the user has entered with the real url being something like this:-

 

http://www.mysite.com/mysite/mainpage/index.php?Search-Term=blah

 

^^^ This is the url that regularly appears when my form is submitted

 

The thing that gets in the way is the fact that clicking a submit button automatically adds the "?" to the url

 

Here is the code for my form:-

 


        <form id="search_form" style="margin-top:0px;" action="index.php">
        	<input id="search_entry" name="Search-Term" type="text"/>
            <input id="search_button" type="submit" value="Search"/>
        </form>

 

Can anybody please help me out with making a rewrite rule for this?

Link to comment
Share on other sites

I've been searching for the solution to my specific problem but can't find anything that relates to it directly. Similar questions I've asked on the mod_rewrite forum have been ignored. This is my first experience at using rewrite rules for more seo friendly urls. I have managed to get the following ones in place:-

 

RewriteRule ^Latest\.html$ index.php?getbylatest=1

 

RewriteRule ^Number\.html$ index.php?getbynumber=1

 

RewriteRule ^Letter-([a-zA-Z0-9_-]+)\.html$ index.php?letter=$1

 

RewriteRule ^([a-zA-Z0-9_-]+)\.html$ index.php?category=$1

 

These are easy enough but my problem is when I get to rewriting those urls that appear when a user uses my search function. You see my search function is done using a form and wen the submit button is clicked it automatically adds in the query string "?" to the url

 

My search function code is as follows:-

 


        <form id="search_form" style="margin-top:0px;" action="/mysite/testwebsite.co.uk/Search/">
        	<input id="search_entry" name="Search-Term" type="text"/>
            <input id="search_button" type="submit" value="Search"/>
        </form>

 

When the user clicks the submit button I need it to go to the following:-

 

/mysite/testwebsite.co.uk/index.php?Search-Term=blah

 

with "blah" being whatever the user types in, letters or numbers

 

but the url displayed to the user should be /mysite/testwebsite.co.uk/Search/?Search-Term=blah

 

I've tried several rewrite rules for this but can't get none working. Can anybody please take a quick look and suggest one for me that would achieve this.

 

Thanks.

Link to comment
Share on other sites

No need to get feisty.

 

You need to understand, mod_rewrite doesn't actually change the underlying urls, so your form will still need to have its action point to wherever it need be whether that be a nice neat url that gets rewritten to something else, or straight to that something else).

 

Leaving your action as is in your last post, you simply need to rewrite /mysite/testwebsite.co.uk/Search/ to /mysite/testwebsite.co.uk/index.php and pass the querystring params along with it. Something like.....

 

RewriteRule ^mysite\/testwebsite.co.uk\/Search$    mysite/testwebsite.co.uk/index.php [QSA]

 

ps: Those urls don't exactly look valid. Do you access your site via http://somedomain.com/mysite/testwebsite.co.uk ?

Link to comment
Share on other sites

Superb. I'm sure that'll work. That explains a lot and this was the problem I was having because it seems as you say forms do actually need to go somewhere whereas mere links seem a lot easier to rewrite.

 

Those urls are just generic examples I put in. I'm testing the site on my own server before uploaded it somewhere else for my client on their server in which case the urls will be tidier and much shorter.

 

Thanks for the help Thorpe.  :D

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.