【问题标题】:Create static HTML pages from database content?从数据库内容创建静态 HTML 页面?
【发布时间】:2014-08-03 10:06:17
【问题描述】:

我们最近为销售儿童玩具的客户建立了一个 PHP 电子商务网站,自推出以来,他引入了一家促销机构,该机构建议为他的每件产品使用静态 HTML 页面。他拥有超过 2,5000 种产品,因此手动构建每个产品都不是一个现实的选择。

是否可以使用 PHP 从数据库中读取每个产品并根据产品名称创建一个新文件,并使用描述、图像和链接填充模板区域?如果是这样,有人对我可以从哪里开始有任何建议吗?

【问题讨论】:

  • 我知道这不能回答你的问题,但你应该高度考虑像 Varnish (en.wikipedia.org/wiki/Varnish_cache) 这样的解决方案。这比编写自定义解决方案来提取静态页面更容易配置和更改。如果这是针对性能问题。或者您只是想以 zip 或其他方式分发页面?还是为了友好的 URL?
  • 如果促销公司真的相信每个产品实际上都预先编写了 HTML。解雇他们并获得一家对他们在做什么有任何线索的新公司。如果没有,您可以使用 URL 重写从数据库信息动态创建使用模板重写的页面。 (取决于服务器,但对于 Apache,请查看 .htaccessmod_rewrite)以便每个产品都可以模拟搜索引擎友好的 URL,如 http://yoursite.com/products/fly_fish_reel.htaccess 将其重写为 /products.php?p=fly_fish_real 或类似的东西.

标签: php html mysql database seo


【解决方案1】:

我认为最好的方法是使用 url rewrite 让它看起来像你有静态页面,而实际上你没有。但是,如果您遇到数据库性能问题并因此想要缓存文件的静态副本,您也可以使用 ob_ 函数来缓存 PHP 文件。比如说你只想每天读取一次数据库并缓存文件,其余时间只返回静态缓存文件。

伪代码:

$cached_file_path = 'some path for cached file';
//would use filemtime($cached_file_path) and time() to determine if file was
//cached today. could also do it every 3 hours, or whatever
If file has not been cached today or cachefile does not exist, then
{
    ob_start(); // starts recording the output in a buffer
    //do all your database reads and echos
    .....
    $contents = ob_get_contents(); // gets all the output for you to write into a file  
    //save contents to file at $cached_file_path
   ....
   ob_end_flush(); // returns the content to client
   exit;
}
else
{
    //just serve the cached file
    include($cached_file_path);
    exit;
}

显然您必须考虑参数,因为它们会影响缓存文件的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2014-08-10
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多