.htaccess 与动态 php - 子文件夹问题
必要知识
我的 .htaccess 文件重定向如下: domain.com/about 到domain.com/index.php?page=about
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]
“page”变量在 php include 中使用:
<?php include_once($_SERVER['DOCUMENT_ROOT']."/contents/".$page.".html"); ?>
“contents”文件夹仅包含作为内容包含的 .html 文件
好的,这里是问题:
我在“contents”文件夹中有一个“子文件夹”,其中包含我需要访问的其他 .html 文件 现在我尝试像这样重定向: domain.com/subfolder/about 到domain.com/index.php?page=subfolder/about
这有效:
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1
但现在我无法从以下位置访问子文件夹:domain.com/subfolder/ 因为有一个“页面”变量
<?php $page = $_GET['page']; if(!$page) { $page = 'index'; } ?>
Any想法、想法或帮助将不胜感激。
提前致谢
Necessary Knowledge
My .htaccess file redirects like this:
domain.com/about to domain.com/index.php?page=about
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]
The "page" variable is used in a php include:
<?php include_once($_SERVER['DOCUMENT_ROOT']."/contents/".$page.".html"); ?>
The "contents" folder simply contains .html files that are included as the content
Okay here's the problem:
I have a "subfolder" in the "contents" folder with additional .html files that I need to access
Now I'm trying to redirect like this:
domain.com/subfolder/about to domain.com/index.php?page=subfolder/about
This works:
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1
But now I can't access the subfolder from: domain.com/subfolder/ because there is a 'page' variable
<?php $page = $_GET['page']; if(!$page) { $page = 'index'; } ?>
Any thoughts, ideas, or help would be greatly appreciated.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在任何其他重写规则之前...
before any other rewrite rule...
您可以通过
编辑排除特定文件夹:链接到手册
You could exclude specific folder via
Edit: Link to the manual
只需再做一次检查:
如果
$page
只是subfolder
,则/index
将被附加到subfolder/index
。Just do another check:
If
$page
is justsubfolder
,/index
would be appended to havesubfolder/index
.这样,您就不必定义任何目录名称 - 它会将它们全部排除在外。
不过,您可能需要测试尾部斜杠,它可能适用于 /subfolder/ 但不适用于 /subfolder
With this, you shouldn't have to define any directory names - it rules them all out.
You may need to test the trailing slash though, it may work on /subfolder/ but not /subfolder