1

I've rewritten this question now as I don't think the problem is to do with the URL rewrite.

I'm building a site with PHP and Smarty and using fairly common url rewriting using apache's mod rewrite.

Locally, if I visit: example.com/search/?q=test, I get search results for "test". /search is not a real directory, it's forwarding to search.php?q=

When I upload, it's not working. I've enabled rewrite with sudo a2enmod rewrite and the url is updating, the browser shows example.com/search/?q=test in the address bar, doesn't throw a 404, but just serves up my index.php page instead.

I've realised the problem is probably to do with my Smarty caching. I've got a URL variable that triggers the cache to be rebuilt, ?rebuildCacheNow.

If I go to /account, I get the index page, but if I go to /account?rebuildCacheNow the account page shows up.

If I then go back to the index, /, it still shows the account page. Going to /?rebuildCacheNow shows me the home page.

This is my main Smarty template:

{* Smarty *}

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"><![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"><![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"><![endif]-->
<!--[if gt IE 8]><!--><html lang="en"><!--<![endif]-->

{include 'head.tpl'}

<body id="top">

{include 'header.tpl'}

<div class="content-container">
    <div class="container content">
      {include $page.content}
    </div>
</div>

{include 'footer.tpl'}

<script src="{$JQUERY_CDN}"></script>
<script src="{$BOOTSTRAP_JS_CDN}"></script>
<script src="/js/main.js"></script>
{$page.scripts}

</body>
</html>

Every page serves up this template, but I'm using PHP to assign $page.content. So the /buy page sets $page.content = 'buy.tpl' and /search sets $page.content = 'search.tpl'.

But it looks like the main.tpl is being cached - is there a better way of using 1 main outline template and embedding content templates within?

Pete
  • 4,542
  • 9
  • 43
  • 76
  • 1) Did you restart apache after enabling mod_rewrite? 2) What do your rewrite logs say? – EEAA Dec 16 '13 at 01:57
  • I've restarted Apache many times, where should I look for rewrite logs? – Pete Dec 16 '13 at 07:40
  • 1
    Set RewriteLog (default is /var/log/apache2/rewrite.log) and RewriteLogLevel, then restart Apache again. You may need RewriteLogLevel 3 to get enough information. – Andrew Schulman Dec 16 '13 at 08:48
  • Do those longs help at all? It seems to be showing the error.php page for every request – Pete Dec 18 '13 at 01:00
  • It is quite possible your rule never gets used. .htaccess files from a particular directory only get read once the server has determined that what it needs to serve ought to exist in that directory, after the URL to Filename translation. If something before that leads to the server thinking that it should serve error.php, than that is what is being served. Since it appears you have access to the httpd.conf I would suggest you avoid putting rewrite in .htaccess files. – Krist van Besien Dec 18 '13 at 10:22
  • I've switched to using Vhost, the errors have gone from the rewrite log but the problem is still there. I think it's to do with Smarty – Pete Dec 29 '13 at 17:02
  • possible duplicate of [Smarty is not choosing the correct templates to display](http://stackoverflow.com/questions/10575213/smarty-is-not-choosing-the-correct-templates-to-display) – Michael Hampton Dec 29 '13 at 17:14
  • The answer on the possible duplicate page doesn't help, also the question outlines a different problem – Pete Dec 29 '13 at 17:25

1 Answers1

0

Ok I've worked it out. So locally I have the rebuildCacheNow set to go off on every page to ensure I see the changes each time - that's why I didn't notice it.

When I worked out it was to do with the caching of the main.tpl, checking the Smarty docs shows that I can assign a cache_id to the rendered template:

http://www.smarty.net/docsv2/en/caching.multiple.caches.tpl

So when I render the Smarty template, I'm giving it the cache ID based on the hash of the $page.content variable:

// make cache id, based on template path
$cache_id = sha1( $page[ 'content' ] );

$smarty->display( 'main.tpl', $cache_id );
Pete
  • 4,542
  • 9
  • 43
  • 76