Omzy Posted December 30, 2009 Share Posted December 30, 2009 Currently the form submits the following URL: /search.php?company=Microsoft I want the URL to be: /searchresults/Microsoft/ Does anybody know the correct rewrite rule to acheive this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/186634-rewrite-rule-for-form-with-query-parameters/ Share on other sites More sharing options...
cags Posted December 30, 2009 Share Posted December 30, 2009 Generally speaking a RewriteRule would do the opposite of what you describe, it would take the 'neat' URL and call the 'ugly' URL, thus masking the crappy address. Obviously to achieve this would require a lot of messing about with regards to changing the current workings of your site. This may be useful. Quote Link to comment https://forums.phpfreaks.com/topic/186634-rewrite-rule-for-form-with-query-parameters/#findComment-985761 Share on other sites More sharing options...
Omzy Posted December 30, 2009 Author Share Posted December 30, 2009 " it would take the 'neat' URL and call the 'ugly' URL" Well yeah, that's exactly what I'm trying to do here. I've done this for normal links on my site, but this is for a form. By default a GET form appends query parameters to it's ACTION URL, for example even if my form URL was: http://localhost/mysite/search/ then if I submit that form I get something like http://localhost/mysite/search/?company=Microsoft, but I'd rather have http://localhost/mysite/search/Microsoft I want the ugly URL turned into a nice URL. Quote Link to comment https://forums.phpfreaks.com/topic/186634-rewrite-rule-for-form-with-query-parameters/#findComment-985897 Share on other sites More sharing options...
cags Posted December 30, 2009 Share Posted December 30, 2009 Well yeah, that's exactly what I'm trying to do here. Actually your trying the exact opposite of that, this is a common thing that seems to trip people up, including myself. The link I provided should give you the framework for getting the URL in the users browser to appear as http://localhost/mysite/search/Microsoft instead of as http://localhost/mysite/search/?company=Microsoft you then need to decide what to do from there. You will probably have to make another RewriteRule to forward this new address back to the old one, but as a proxy rather than redirect. I'm not entirely sure how you can do this without causing a loop. I may experiment, if I work it out I'll get back to you. Quote Link to comment https://forums.phpfreaks.com/topic/186634-rewrite-rule-for-form-with-query-parameters/#findComment-985917 Share on other sites More sharing options...
cags Posted December 30, 2009 Share Posted December 30, 2009 I tested this on index.php in the root folder... RewriteCond %{QUERY_STRING} ^company=([a-zA-Z0-9]+)$ RewriteRule ^index\.php$ /search/%1? [R=301] RewriteRule ^search/([^/]+)/? /index.php?search=$1 [L] The first rule redirects the URL in the users browser and the second passes the new URL back to the original page on the server side without the user being aware. Quote Link to comment https://forums.phpfreaks.com/topic/186634-rewrite-rule-for-form-with-query-parameters/#findComment-985922 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.