Smarty with htaccess

If you are using PHP with SMARTY  and want to quickly convert your ugly URLs to pretty URLs, it is best to use a detailed ruleset for mod_rewrite to process. The alternative is to say “if the requested file does not exist, rewrite to a particular script”. This means that requests for images, CSS files, etc. will still return the correct file. Everything else will be sent to the script to process where the requested URL will be available under the server REQUEST_URI variable. To use this method, you would need to create a .htaccess file in your domain root (or wherever you want the rewrite to apply) with the following inside:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]

Replace index.php with the name of whatever script you wish to use. Sticking with the PHP example, I could then create the index.php file as follows:

<?php
$requested = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI'];
 
$smarty=new Smarty();
 
switch ( $requested ) {
 
        case 'latest':
               require_once 'latest_products.php';
               $content=$smarty->fetch(latest.tpl);
               break;
 
        case 'listing':
               require_once 'list_products.php';
               $content=$smarty->fetch(list_products.tpl);
               break;
 
        default:
               include 'default.php';
               $content=$smarty->fetch(default.tpl);
}
 
$smarty->assign(‘header’,$smarty->fetch(header.tpl));
$smarty->assign(‘footer’,$smarty->fetch(footer.tpl));
$smarty->assign(‘bodyContent’,$content);
$smarty->display(main.tpl);
 
?>

 
main .tpl

 {$header}

{$bodyContent}

{$footer}

 

If you need any projects or customization in SMARTY and PHP  , please send me an message via contact form

  Get updates in mail :

Post a Comment

Your email is never published nor shared. Required fields are marked *