在我的cms中,我将所有页面存储在数据库中。如果您当前请求页面,它的工作方式如下:
请求的URI:www.xyz.com/site.html
.htaccess:mod_rewrite创建:/index.php?q=site
index.php:在数据库中查找条目site
。
要清理我希望拥有以下网址的网址:www.xyt.com/about/site.html
或www.xyt.com/about/groups/groups.html
在我的数据库中,每个名为owner
的页面的条目代表父站点。
对我来说问题是'文件夹'的数量不固定。
所以我认为我应该在.htaccess中将www.xyt.com/about/site.php
更改为/index.php?q=about-site
,然后编写一个PHP函数,找到具有父site
的网站about
RewriteRule会是什么? 我是一个好方法还是有其他(更好)的方式?
答案 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]