Creating canonical search result URLs with mod-rewrite

277 views Asked by At

I have a search engine on my site with two search parameters, location and query. I want to show the results on a page with the canonical URL of /search/location/query.

I tried aiming the search at a non-existant PHP file which I could then rewrite using my .htaccess:

# search bar rewrites
RewriteRule ^search.php?query=([A-Za-z_-]+)&location=([A-Za-z_-]+)$ /search/$1/$2/ [R]

This doesn't seem to be working, however, and after setting my search form to post GET to search.php I just get a 404. I would prefer not to have to use Javascript to submit the form directly to the canonical url - any way I can rewrite it dynamically with mod rewrite?

1

There are 1 answers

1
Shad On

Wouldn't the better approach be to change the form from a get to post?

Generally pretty URIs are for actual locations rather than search results.