I've recently gotten several e-mails asking how I created my friendly search URLs here on alexking.org (also used on the Crowd Favorite web site, etc.); I figured others might be interested in this as well.
It's really rather simple, there are just a couple of places to make changes. Note: you must be using nice permalinks with an Apache web server for this to work as described below.
.htaccess File
Add this mod_rewrite rule in your .htaccess file:
RewriteRule ^search/(.+)?$ /index.php?s=$1 [QSA,L]
UPDATE: Mark informs me that this has actually been built in to WP since 1.5 (meaning this step is not necessary) - who knew? Not me apparently. 🙂
Theme
Add an onsubmit
handler to the form
tag in the search form in your theme:
<form action="<?php bloginfo('wpurl'); ?>/index.php" method="get" onsubmit="location.href='<?php bloginfo('home'); ?>/search/' + encodeURIComponent(this.s.value).replace(/%20/g, '+'); return false;">
Then... you're done. Kinda anti-climactic, but it's a pretty simple little change.
One thing I've found handy about this is the ability to simply type:
https://alexking.org/search/your+search+terms
to search my site.
There's more than one way to skin a cat, suggestions and improvements to this are welcome in the comments.
Hmm… I can’t seem to redirect the ugly URL to friendly URL. Can I make /index.php?s=term redirect to /search/term?
[…] Ever wondered how you could transfer any searches from ?s=TERM to /search/TERM , then read below or visit Alex King’s approach. […]
That’s good changes for wordpress search. I can write post in spanish in my blog :)) Thanks Alex.
Hi, Alex. I have a WordPress plugin for that. I’ve submitted it already to the official repository, I just haven’t received the approval mail yet. I think this would be good for those who can’t use
.htaccess
or do not havemod_rewrite
. 🙂This ought to be in the WordPress core. Too bad they’ve just feature frozen WP 2.3.
[…] vÃa: AlexKing […]
Mark jaquith wrote a plugin out which has done this since WP1.5 and doesn’t rely on your user having JavaScript enabled. Check it out: http://txfx.net/code[...]nice-search/
nice little trick. funny thing is that i changed the form and not the .htaccess file, and it still worked for me.
thanks for that.
Thanks Alexking
My version is here, which I’ve had going since 2005. I do a little extra stuff like remove searches that include “.dll” or “.exe” or some other terms common from viruses or spammers.
Oh, and mine just searches on any 404 result, so I can type “http://nslog.com/404 search” in my browser and get the correct search results, similar to your method. If there’s one result, the user will be taken there directly, which helps if a few characters in a URL are snipped at the end or I remove a “-” or something from a page’s slug.
I hate to break it to you, but this is already in the core of WordPress. There’s no need to edit your .htaccess file and add that line. It’ll handle it right outta the box. 😉
I tried it, the results were weird. when I searched something, it forwarded me to the first revelant post of the search terms. How did this happen?
@Viper007Bond
Editing
.htaccess
and templates or using plugins would redirect users to the cruft-free URL instead of using a query string (browsers’ default behavior) with GET forms.Those methods mentioned above wouldn’t even be possible without WordPress having the
/search/term
permalink structure in its core. Unless of course you add some more lines to that simple.htaccess
code above.Thanks, Alex, for this useful tip! Implemented and tested within minutes. Very handy.
Rich
Thanks Alex. Its really a good tip. i have tested myself and it really worked awesome.
[…] – Cambios en el fichero .htaccess Ahora tienes que abrir el fichero .htaccess y añadir esto: RewriteRule ^search/(.+)?$ /index.php?s=$1 [QSA,L] Listo, el buscador de tu blog para wordpress ya es mas bonito y será mejor visto por los buscadores Asi quedan las urls: Sitelinks , mapas, fiesta Gracias a Alexking […]
[…] nuestro blog será mas "usable" y tendrá algun punto a favor cara a los bucadores. Via: alexking.org/blog/2007/08/30/friendly-search-urlsetiquetas: wordpress, seo, buscador sin comentariosen: tecnologÃa, internet negativos: 0 karma: […]
Under the circumstance that you want your search results showing up in multiple pages (say 15 results per page, as I do) I’d like to suggest the following modification:
Rewrite the following rewrite rule
RewriteRule ^search/(.+)?$ /index.php?s=$1 [QSA,L]
to read
RewriteRule ^search/(.+)?/page/?$ /index.php?s=$1&paged=$2 [NC,QSA,L]
Now, not only will searching for a term as /search/term give you a list (Example), but search terms involving many more results can be paged and accessed as search/term/page/number (Example).
Enjoy!
You’ll need to create those URL links in your theme or in a plugin as well.
[…] Olayı Åžekilli Arama Adresleri Alex King burada http://www.site.com?s=aranan ÅŸeklinde olan arama adreslerimizi ( url ) nasıl http://www.site.com/search/aranan […]
[…] Friendly Search URLs | alexking.org – Use .htaccess file to make more functional and friendly URLs. […]
I am not sure why the part of htaccess dont work for me
The
.htaccess
part isn’t necessary, as WordPress’ internal rewrite engine already has that rule. You can use a/search/foo
URL on any WordPress blog with “fancy” permalinks. The cool part about Alex’s solution is using JavaScript to intercept and do the rewriting for you (as opposed to my plugin which uses a PHP-based redirect to point?s=X
to/search/X
[…] King has a sweet little writeup about how to set your WordPress blog up to have friendly search URLs. What makes them so nice is […]
Great thanks for this. It make search result url look more beauty.
I could comfirm what Mark informed you, mod_rewrite rule has no need for WP 2.2.2 as I tested.
Thanks Alexking 🙂
Hi Alex
I must be missing something … what the big deal with the way you set your URLs?
You’ll get just as good ranking results if you people set permalinks in the WP admin panel (Options > Permalinks) to:
/%postname%.php
Job done! If I’m wrong, please someone feel free to correct me …
Cheers
Nick 🙂
Er … slight grammar typo in that last comment …
I meant to say …
“You’ll get just as good ranking results if you set permalinks …”
It came across as though I was talking down from “on high” which isn’t what I meant … apologies.
[…] I have added in a modified version of Alex King’s Friendly Search URLs. […]
This is great.
Not sure if this has already been mentioned, but taking this one step further it is easy to use some very simple JavaScript to re-populate the search field with the user’s search terms in a manner similar to the way Google retains the search terms you enter in all subsequent page reloads. Simply set the following function to be called
onload
of thewindow
and you should be all set:function setDefaultSearchValue () {
var x = location.href.replace(/\+/g, '%20').match(/search\/(.*)$/);
if (x) {
document.getElementById('searchform').s.value = decodeURIComponent(x[1]);
}
}
sweet, thanks very much!
[…] 翻译自:Friendly Search URLsï¼Œæœ‰åˆ èŠ‚ï¼ […]
[…] de mensen dewelke ook WordPress gebruiken vond ik op het internet een handige handleiding waarmee je de URL van de zoekfunctie mooier kunt maken. Hiermee bedoel ik concreet dat er in plaats […]
I can confirm that the .htaccess part is indeed not needed for recent WordPress versions.
Anyway, thanks for sharing the searchform enhancement!
[…] 翻译自:Friendly Search URLsï¼Œæœ‰åˆ èŠ‚ï¼ […]
Thanks for your information. It’s very usefull
Thank you for info
The part I am anxious to understand is how you get the search term that was typed in to display on the page. For example – on the page https://alexking.org/search/test it says ‘Results for: test’ at the top of the page. I feel like I must be missing something so simple here – but your post about the search URLs is as close as I have managed to get.
Thank you!
This was easy to implement thank you!
[…] automatically whenever someone searches your blog? Thanks to a little technique developed by Alex King, we can add some code to our new widget to do just […]
[…] 翻译自:Friendly Search URLsï¼Œæœ‰åˆ èŠ‚ï¼ […]
Jeanne,
The reason for that, is that the query variable ( parameter ‘q’ having the value ‘test’), is written inside the title HTML tag, inside the head of HTML.
Hope it helps!
thank you
[…] Makalenin aslına ulaÅŸmak için tıklayın. […]
Hey we got the sweet url now with search/searchterm…
Is it possible to replace search by another word?
that’s a neat trick.. cool Alex.
.htaccess is such a wonderfull & mighty tool 😉
[…] [upmod] [downmod] Friendly Search URLs | alexking.org (alexking.org) 0 points posted 1 year, 8 months ago by thilak tags url howto search imported […]
Alex, great tip! We’re in the process of incorporating this in our website. Keep up the good work!
[…] King burada http://www.site.com?s=aranan ÅŸeklinde olan arama adreslerimizi ( url ) nasıl http://www.site.com/search/aranan […]
🙂