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

时间:2015-01-20 21:58:38

标签: php .htaccess mod-rewrite content-management-system

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

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

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

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

  4. 要清理我希望拥有以下网址的网址:www.xyt.com/about/site.htmlwww.xyt.com/about/groups/groups.html

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

    对我来说问题是'文件夹'的数量不固定。 所以我认为我应该在.htaccess中将www.xyt.com/about/site.php更改为/index.php?q=about-site,然后编写一个PHP函数,找到具有父site的网站about

    RewriteRule会是什么? 我是一个好方法还是有其他(更好)的方式?

1 个答案:

答案 0 :(得分:2)

使用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=$1 [L]