SEF / SEO search for your sNews website

SEF / SEO search for your sNews website

Here's a nifty mod for your sNews CMS, or any website really, this is one of the easier mods and should be done for any sNews install in my opinion. This mod will take your search form's search_query variable and reformat it to a sexy (aka SEF) URI.

This will make it easy for your visitors to bookmark searches and also to use Google Analytics to set up a filter to see what folks are searching for... more on the Google Analytics filter later, let's get rolling on the mod.

To try it out, simply look to your right, my search engine now uses this mod.

<!--IMPORTANT!
Please note that if your website uses non-ASCII characters, this likely won't work for you.
END IMPORTANT!-->

As always, back up your snews.php file and work on a copy. Now around line 72 add the following code just above the function s():

//Rewrite the search
if (isset($_POST['search_query'])) {
// Get the POST search query and clean it up
$squery = strip_tags(strtolower(cleanXSS($_POST['search_query'])));
$squery = preg_replace("/\s\s+/", " ", $squery);
$squery = trim($squery);

    // If there's anything left, we'll redirect to a SEF URI
    if ($squery != '') {
        header( 'Location: '._SITE.'search/'.urlencode($squery).'/' );
        exit;
    }
}

OK, now around line 118, we need to add search to the SEF hardcoded links or else it'll throw a 404 header, now find

$l['cat_listSEF'] = 'archive,contact,sitemap,login,administration,admin_category,admin_article,article_new,extra_new,page_new,snews_categories,snews_articles,extra_contents,snews_pages,snews_settings,snews_files,logout,groupings,admin_groupings';

add then add search to the list, like below:

$l['cat_listSEF'] = 'archive,contact,search,sitemap,login,administration,admin_category,admin_article,article_new,extra_new,page_new,snews_categories,snews_articles,extra_contents,snews_pages,snews_settings,snews_files,logout,groupings,admin_groupings';

Now around line 529, within the function center(), insert the new code below the globals:

global $categorySEF, $subcatSEF, $articleSEF;
    // begin new code
    $url = explode('/',clean(cleanXSS($_GET['category'])));
    if ($url[0] == "search") {
        $search_query = $url[1];
    }
    // end new code

Now around line 543, still within the function center(), find the following code:

case isset($_POST['search_query']):
    search(); return; break;

Now CHANGE the code to:

case isset($search_query):
    search($search_query); return; break;

Almost done!, now find the function search() around line 1457, locate the following lines:

//SEARCH ENGINE
function search($limit = 20) {
    $search_query = clean(cleanXSS($_POST['search_query']));

Now CHANGE the code to:

//SEARCH ENGINE
function search($search_query,$limit = 100) {
    $search_query = clean(cleanXSS($search_query));

That's it, now upload your snews.php file and give it a go. As always, no warranties, I'm not responsible for you breaking anything with my code :P, but please let me know if you have any problems and I'll do my best to assist you.

Tags

 

You might like

Comments


Thanks, very useful :)

Maybe you could use POST for the tags mod also?


Hi Sven,

Yep, the next version of the tags mod does in fact use POST, the same as this mod, I've actually got it working on my site this way already, I just need to write up the tutorial :)

Stay tuned. Tags 2.0 is coming within a few days.


Hi Matt

It don't work for me I double check it on Clean snews 1.7 and my one multi mod... 1.7 version.

It all ways gows to start page... showing start page... What is wrong? maybe this?

// begin new code
$url = explode('/',clean(cleanXSS($_GET['category'])));
if ($url[0] == "search") {
$search_query = $url[1];
}
// end new code

Can You help me? cant manage this esue...
(sorry for my week english)


UPDATE:

remove the line
$squery = preg_replace("/\W/", " ", $squery);
and it should work, I'll update the tut now.
Thanks for pointing it out to me K4iTO, your English is fine.


eh eh another great Mod I missed.
That one is great too.
More of that, your articles are pleasant to read.
Thanks a lot Matt.


I have found an issue.
A small one, don't worry. :-D
It concerns accented chars.
If someone look for "annees 50"
it retuns this SEF URI: search/annees+50/.
Great!
But those bloody foreigners have accented chars in their *^p£! language,
so, if a french guy looks for "années 50" it returns:
search/ann+es+50/.
No problem with that URI, but,
HTML being displayed to say how many articles have been found do not display the accented char "é".
"100 réponses correspondent à votre requête : ann es 50.".
There's a hole!
eh eh...


Sven, just comment out or remove the line;

$squery = preg_replace("/\W/", " ", $squery);

There seems to be too many issues with language encodings, it's just there to help clean up the query and it's not necessary, so I'm going to remove it altogether.


Okey Matt. It does the job now.
Thanks a lot.


Ok I have don it all over agein ...

It start's to show all searches corect but...

A franch, polish also have problem with encoding ex.

Polish word "huśtawki"
it showns in search term "There are no results for your query; hu�tawki"
and in the url line i have this "/hu%B6tawki/"

Hmm how can i handle UTF-8 With polish sings coding?

I dont have "$squery = preg_replace("/\W/", " ", $squery);" in the code :D


Hmmm, I'm not able to duplicate that on my site, maybe urlencode is mucking it up...

Are you sure your PHP encoding is UTF-8?

You can try what Philippe did recently, set the locale manually at the top of your snews.php file, that may work?

setlocale(LC_ALL, 'pl_PL.utf8');


ok i'll try :D


nope it don't help me :/


Damn, I'm not sure, I've tested it here on my site, I've tested it on my "sandbox" site, and I've tested it on Philippe's site too, all of them accept huśtawki just fine.

Maybe it's the web server itself with the redirect?, are you using Apache, IIS, Litespeed?


Thanks for your excellent efforts, but i just can't make this work.. (Hebrew site)


Yeah, sorry Roie, I wish I knew, there appears to be a problem with url encoding and non Latin character sets, but to be honest, that really isn't my best area of knowledge :/


Hi.
Great mod! Polish signs works fine on my site.
Only title looks little funny (look at firefox tab http://f.imagehost.org/0561/searchmod.png).
Will be great to display search phrases in title, description and keywords.


Toolman, see this page;
<a href="http://mdj.us/snews-cms/hacks-mods/fix-duplicate-meta-description-errors-in-snews-with-this-quick-hack/">http://mdj.us/snews-cms/hacks-mods/fix-duplicate-meta-description-errors-in-snews-with-this-quick-hack/</a>


Hi Matt,
I'm succesfully using your search with a German language site. It works great. Recently, I made a small modification, to enable the search query via GET method as well. I did this to so that I could get the searchplugin I made to work both in IE and firefox (post method will only work in firefox, IE is special as usual) ... Thought I'd share (hope its ok here, otherwise just delete or rewrite) ...


if (isset($_POST['search_query']) or isset($_GET['s'])) {
// Get the POST search query and clean it up
if (isset($_POST['search_query'])) {
$squery = strip_tags(strtolower(cleanXSS($_POST['search_query'])));
} elseif (isset($_GET['s'])) {
$squery = strip_tags(strtolower(cleanXSS($_GET['s'])));
}

Then, I made a new file called browsersearch.xml with the following content:


<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Your Site Title</ShortName>
<Description>Your Site Description</Description>
<Image height="16" width="16" type="image/x-icon">http://www.your-site.com/favicon.ico</Image>
<Url type="text/html" method="get" template="http://www.your-site.com/?s={searchTerms}"/>
</OpenSearchDescription>

and finally, changed my index to include a new meta tag ...

<link rel="search" type="application/opensearchdescription+xml" href="browsersearch.xml" title="Demo Site" />

greetz
tina


Thanks for sharing Tina!

I formatted your code too, I really need to implement the bbcode tags and auto-formatting.

Comments are closed. No new comments allowed.

Copyleft 2002 - 2017 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top