Search Form clean URL writing

New to mod_rewrite? This is a good place to start.

Search Form clean URL writing

Postby pushpinderbagga » Wed Jul 01, 2009 11:31 am

Hello there

below i have a clean form...

Code: Select all
<form action="http://www.my-domain.com/search.php" method="get">
<input type="text" name="q" id="search" value="" />
</form>


if I search something example cricket or greg timmons... then the page goes to

http://www.my-domain.com/search.php?q=cricket

or

http://www.my-domain.com/search.php?q=greg+timmons

but I want the URL to be

http://www.my-domain.com/search/cricket/

and

http://www.my-domain.com/search/greg timmons/ (the space here could be replaced by a + or an _ )

any idea how to accomplish this... I tried



Code: Select all
RewriteRule ^search/([a-zA-Z0-9_-]+)$ search.php?q=$1 [NC]
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ search.php?q=$1 [L,NC]


but no go... even the direct url examples like

http://www.my-domain.com/search/ANYTHING-HERE/

dont open and give a not found error ?

please help.
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm

Postby richardk » Fri Jul 03, 2009 9:07 am

You can't control the URL of a GET form with mod_rewrite, but you can redirect /search.php?q=keyword to /search/keyword/. You can control the form with JavaScript (onSubmit) and i think you should if you are going to do the redircet to save a request for those with JavaScript available.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?q=([^&]+)(&.*)?$ [NC]
RewriteRule ^search\.php$ /search/%2/? [R=301,L]

RewriteRule ^search/([^/]+)/?$ /search.php?q=$1 [L]
Last edited by richardk on Mon Jul 06, 2009 12:31 pm, edited 1 time in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

thanks

Postby pushpinderbagga » Fri Jul 03, 2009 12:41 pm

I will try this and post a reply to the full solution I install :D
pushpinderbagga
 
Posts: 36
Joined: Fri May 22, 2009 9:43 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 100 guests

cron