View previous topic :: View next topic |
Author |
Message |
felix Smarty n00b
Joined: 13 Sep 2004 Posts: 2
|
Posted: Mon Sep 13, 2004 3:55 pm Post subject: Search Engine Friendly URLS |
|
|
Hi all, this may not be the most elegant or the best approach but this function plugin works for me. It will convert a standard URL into a search engine friendly one. So 'index.php?key1=value1&key2=value2' becomes 'index.php/key1/value1/key2/value2'. By passing it a variable you can turn it on or off so your site can be easily configured.
I have also included the code that I use to get the variables.
This goes in function.html_url.php in the plugins directory:
Code: | function smarty_function_html_url($params, &$smarty){
require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
$url = '';
$text = '';
$sef = false;
$target = '';
foreach($params as $_key => $_val){
switch($_key){
case 'link':
$$_key = $_val;
break;
case 'target':
$$_key = " target=\"" . $_val . "\"";
break;
case 'sef':
$$_key = (bool)$_val;
break;
case 'url':
$$_key = smarty_function_escape_special_chars($_val);
break;
default:
if(!is_array($_val)) {
$extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
} else {
$smarty->trigger_error("html_url: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
}
break;
}
}
if($sef){
$url= preg_replace("/[\&\?\=]/","/",$url);
}else{
$url = $url;
}
return "<a href=\"" . $url . "\"" . $target . $extra . ">" . $link . "</a>";
} |
and this goes where ever:
Code: | if(SITE_SEF_URLS == 'true'){
$vars = explode('/', $PATH_INFO);
array_shift($vars);
for($i=0,$n=sizeof($vars); $i<$n; $i++){
$_REQUEST[$vars[$i]] = $vars[$i+1];
$i++;
}
$smarty->assign('sef', true);
}else{
$smarty->assign('sef', false);
}
|
so in the template you put this:
Code: | {html_url url=$passed_url link=$link_text sef=$display_sef_urls} |
comments are welcome of course.
-felix |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Wed Sep 15, 2004 6:10 am Post subject: |
|
|
why dont using apache mod_rewrite? |
|
Back to top |
|
felix Smarty n00b
Joined: 13 Sep 2004 Posts: 2
|
Posted: Wed Sep 15, 2004 6:46 am Post subject: mod_rewrite |
|
|
Yeah, but for the what i was doing i wanted the ability to turn off (or on) the search engine friendly urls from an admin section. This then enables me to have them rewritten in the template.
If there is a better solution it would be good to hear about it. I just hijacked the html_image plugin quickly. in fact, i haven't really tested it much!
-felix |
|
Back to top |
|
Dr DLP Smarty Rookie
Joined: 03 Oct 2004 Posts: 5 Location: France
|
Posted: Sun Oct 03, 2004 3:24 pm Post subject: |
|
|
Hi
I use filters to rewrite my url, it is very convenient.
First the rewrite function :
Code: | function rewrite_urls($tpl_source, &$smarty)
{
$before = array(
"'(?<!/)index.php\?game_id=([a-zA-Z0-9]*)'",
);
$after = array(
"jeu-java-\\1.html",
);
$tpl_source = preg_replace($before,$after,$tpl_source);
return stripslashes($tpl_source);
} |
Then I use another one to display the file:
Code: | function template_display($file)
{
global $template;
$template->load_filter('outpout', 'rewrite_urls');
$template->register_outputfilter("rewrite_urls");
$template->display($file);
} |
It is very faster than using ob_*
PS : I use $template instead of $smarty, I just can't live without the phpBB code |
|
Back to top |
|
rich201 Smarty n00b
Joined: 06 Mar 2005 Posts: 1
|
Posted: Sun Mar 06, 2005 12:36 pm Post subject: Re: Search Engine Friendly URLS |
|
|
[quote=
and this goes where ever:
Code: | if(SITE_SEF_URLS == 'true'){
$vars = explode('/', $PATH_INFO);
array_shift($vars);
for($i=0,$n=sizeof($vars); $i<$n; $i++){
$_REQUEST[$vars[$i]] = $vars[$i+1];
$i++;
}
$smarty->assign('sef', true);
}else{
$smarty->assign('sef', false);
}
|
so in the template you put this:
Code: | {html_url url=$passed_url link=$link_text sef=$display_sef_urls} |
[/quote]
Hi - i tried getting this to work on my own smarty site, but it doesn't change the URL - i created the first plugin but I'm not sure whether I put the second part in the correct place (I put this in the main index.php) - then the {html_url tag in the main template.html but the URL doesn't change.
What could I be doing wrong ?
Thanks |
|
Back to top |
|
jafar_salam Smarty n00b
Joined: 21 Sep 2005 Posts: 2
|
Posted: Mon Sep 26, 2005 2:09 pm Post subject: |
|
|
hi;
anybody got this code working ?
thanks |
|
Back to top |
|
human Smarty Rookie
Joined: 21 Oct 2005 Posts: 12 Location: Helldorado
|
Posted: Mon Jun 05, 2006 10:10 am Post subject: |
|
|
it is mine very beta solution (test it before using)
i created it in 15minutes ;] so boys and girls lets go:
link structure like this: domain.com/com/tab/id/pg
Code: |
function smarty_function_seolink($params, &$smarty)
{
global $rewriteEngine,$path,$rewriteSep,$rewriteEnd;
// http://www.domain.com/com/tab/id/pg
$url = parse_url($params['link']);
$link =''.$path.'';
if($url['scheme'] == 'http') $link=$params['link'];
else
{
if($rewriteEngine==1)
{
if(!isset($params['alt']))
{
$x = explode('?', $params['link']);
$y = explode('&',$x[1]);
$i = count($y); $j = count($x);
if($j>1)
{
$q=1;
foreach ($y as $z)
{
$o = explode('=',$z);
if($i==$q) $link .=''.$o[1].''.$rewriteEnd.''; else $link .=''.$o[1].''.$rewriteSep.'';
$q++;
}
}
else { $v = explode('.', $params['link']); $link .=''.$v[0].''.$rewriteEnd.''; }
}
else
{
$link .=''.$params['alt'].''.$rewriteEnd.'';
}
}
else
{
$link .=''.$params['link'].'';
}
}
if (empty($params['assign'])) return $link;
else $smarty->assign($params['assign'],$link);
}
|
$rewriteEngine,$path,$rewriteSep,$rewriteEnd are globals:
Code: |
$rewriteEngine = 1;
$rewriteSep = ',';
$rewriteEnd = '.html';
$path = '/yourpath/';
|
in template:
Code: | {seolink link='index.php?com=page&id=2'} |
result: page,2.html
or
Code: | {seolink link='index.php?com=page&id=2' alt='about'} |
result: about.html
then in php scripts we must decode link
Code: |
// http://www.domain.com/com/tab/id/pg
$requesturi= $_SERVER['REQUEST_URI'];
$url=explode($path,$requesturi);
$x=explode($rewriteSep,$url[1]);
$j=count($x);
if($j>1) { if(isset($x[0])) $_COM=$x[0]; } else { $v = explode('.', $url[1]); $_COM=$v[0]; }
if(isset($x[1])) $_TAB=$x[1];
if(isset($x[2])) $_ID=$x[2];
if(isset($x[3])) $_PG=$x[3];
|
.htaccess file (the same as in Mambo/Joomla project ;] )
Code: |
RewriteEngine On
RewriteBase /yourpath
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|gif|gif|swf|fla|js|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
|
what do you thinking about this?
you can fix this codes or write new |
|
Back to top |
|
yorganic Smarty n00b
Joined: 25 Jul 2006 Posts: 1 Location: Lancashire, NW, UK
|
Posted: Tue Jul 25, 2006 9:47 pm Post subject: great search engine optimisation tactic |
|
|
Mmm, quite useful on reseller hosting where you aren't given access to apache mod re-write. _________________ Yorganic Search Marketing
________________
Search Engine Optimisation |
|
Back to top |
|
mrLenin Smarty n00b
Joined: 24 Nov 2006 Posts: 4
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Nov 24, 2006 3:05 pm Post subject: Re: great search engine optimisation tactic |
|
|
yorganic wrote: | Mmm, quite useful on reseller hosting where you aren't given access to apache mod re-write. |
Also quite useful on cheap hosting where you don't have access to php.
Also quite useful when you don't have any host at all.
What do you want to tell us? |
|
Back to top |
|
felixpfeiffer Smarty Rookie
Joined: 07 Jun 2006 Posts: 5
|
Posted: Tue Nov 28, 2006 1:07 pm Post subject: Re: great search engine optimisation tactic |
|
|
messju wrote: |
Also quite useful on cheap hosting where you don't have access to php.
Also quite useful when you don't have any host at all.
What do you want to tell us? |
I think he wants to tell you, that there are hosters, that don't have the mod_rewrite activated. So you can do with the htaccess whatever you want, it doesn't work |
|
Back to top |
|
Gee Smarty n00b
Joined: 25 Apr 2009 Posts: 2
|
Posted: Sat Apr 25, 2009 12:47 am Post subject: newbie |
|
|
hey where do u put the code....like everyone asking....help us.... |
|
Back to top |
|
|