数据库 .htaccess 配置中的 CMS 页面

CMS Pages in Database .htaccess configuration

在我的 CMS 中,我将所有页面存储在数据库中。如果您当前请求一个页面,它的工作方式如下:

  1. 请求的 URI:www.xyz.com/site.html

  2. .htaccess: mod_rewrite 创建: /index.php?q=site

  3. index.php:在数据库中查找条目 site

要清理 URL,我喜欢这样的 URL:www.xyt.com/about/site.htmlwww.xyt.com/about/groups/groups.html

在我的数据库中,每个名为 owner 的页面都有一个条目,代表父站点。

我的问题是 'folders' 的数量不固定。 所以我想我应该在 .htaccess 中将 www.xyt.com/about/site.php 更改为 /index.php?q=about-site,而不是编写一个 PHP 函数来查找站点 site 与父 about

重写规则是什么? 我认为这是一个好方法还是有其他(更好)的方法?

使用 mod_rewrite 将 foo/bar/about/site/etc.html 更改为 index.php?q=foo-bar-about-site-etc 比 PHP 更难。只需在 php 中执行此操作,获取 $_GET['q'] 变量并使用 / 标志将字符串分解为数组或其他内容。这种方式也更好,因为您可以确定 / 字符是保留的,并且您最终不必解析 /foo-bar/about/site 之类的东西。规则看起来像:

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /+index\.php\?q=([^&\ ]+)
RewriteRule ^ /%1.html [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /index.php?q= [L]