Jump to content

How do I pass a GET variable so it's URL friendly?


Recommended Posts

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?

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>

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>

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.

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

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 :)

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.