Guest Posted January 27, 2011 Share Posted January 27, 2011 Here's my search form: <form action="search" method="get" enctype="multipart/form-data"> <input type="text" name="string" maxlength="100" /><br /><br /> <input type="submit" value="Search" /> </form> When I submit it, the URL is search.php?string=Bleh What I would like is for the URL to be search/string/Bleh I'm already doing this with some other variables, like search/tag/Bleh and search/author/Bleh, instead of search.php?tag=Bleh and search.php?author=Bleh. However, those ones are passed via a link, not a form. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/ Share on other sites More sharing options...
Guest Posted January 27, 2011 Share Posted January 27, 2011 Doh! I need to do more research before asking a question: http://alexking.org/blog/2007/08/30/friendly-search-urls For me, I simply needed to change my form to: <form action="search" method="get" enctype="multipart/form-data" onsubmit="location.href='search/string/' + encodeURIComponent(this.string.value).replace(/%20/g, '_'); return false;"> <input type="text" name="string" maxlength="100" /><br /><br /> <input type="submit" value="Search" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165857 Share on other sites More sharing options...
Guest Posted January 27, 2011 Share Posted January 27, 2011 Just in case anyone ever stumbles across this and is wondering, I modified the code to make the URL more search engine friendly, replacing all characters that aren't URL-safe with underscores: <form action="search" method="get" onSubmit="location.href='search/string/' + this.string.value.replace(/[^A-Za-z0-9-._~]/g, '_'); return false;"> <input type="text" name="string" maxlength="100" /><br /><br /> <input type="submit" value="Search" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165864 Share on other sites More sharing options...
QuickOldCar Posted January 27, 2011 Share Posted January 27, 2011 Only problem with that is when have multiple bad characters and or spaces, will get a pile of ________ in one spot I'd replace: __ with _ ___ with _ ____ with _ just for good measure Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165867 Share on other sites More sharing options...
Guest Posted January 27, 2011 Share Posted January 27, 2011 Any suggestions on how to do that? Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165868 Share on other sites More sharing options...
QuickOldCar Posted January 27, 2011 Share Posted January 27, 2011 I just clean them after the get and not in the form. I didn't care whats left in the url, as long as the form gets the right data. So do str_replace after on that get would be my suggestion, but might as well keep what you already have too. I'm not even entirely sure why you are searching those type url's anyway, why not use a normal search and pull the matching words from mysql fields. You know, like a normal search. Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165889 Share on other sites More sharing options...
Guest Posted January 27, 2011 Share Posted January 27, 2011 Define normal search... I'm a noob Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165890 Share on other sites More sharing options...
QuickOldCar Posted January 27, 2011 Share Posted January 27, 2011 From what I see you are replacing those characters with an underscore, does your search work with an underscore? Did you do rewrite rules to make them work right? It's cool if it works, my urls are miles long sometimes, but I never cared how they look, as long as they work correct, but that's just me. Here's an example my basic search url just to show you http://dynaindex.com/search.php?s=%2Bfree+%2Bphp+%2Btutorial+-download+-script&page=1 Now sure I can do lots to make it look different, but for my searches I need those + and - to include and exclude, and if didn't know already spaces break hyperlinks when copy/paste them. So I just leave the urlencoded %2B Maybe you have a simpler setup and just single words and spaces Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165893 Share on other sites More sharing options...
Guest Posted January 27, 2011 Share Posted January 27, 2011 Well, I'm using mod_rewrite like this: RewriteRule ^search/string/(.+)(/)?$ search.php?string=$1 So when someone goes to search/string/Silent_Hill__Downpour they are actually accessing search.php?string=Silent_Hill__Downpour. I read it's more search engine friendly this way. Although crawlers won't be submitting forms, they can access the same page by going to search/tag/Silent_Hill__Downpour or search/author/Dave via a number of links on the site. It all works the same way, and I like to keep it consistent. I don't have any need for advanced searches such as - to leave out words or " to include specific phrases. At least not for now. There's too much on my plate Quote Link to comment https://forums.phpfreaks.com/topic/225817-how-do-i-pass-a-get-variable-so-its-url-friendly/#findComment-1165898 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.