.htaccess 与动态 php - 子文件夹问题

发布于 2024-07-29 20:13:54 字数 837 浏览 14 评论 0原文

必要知识

我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红焚 2024-08-05 20:13:59
rewriteRule ^foldername/.*$ - [PT] 
rewriteRule ^foldername/.*$ - [PT] 

在任何其他重写规则之前...

rewriteRule ^foldername/.*$ - [PT] 
rewriteRule ^foldername/.*$ - [PT] 

before any other rewrite rule...

提赋 2024-08-05 20:13:58

您可以通过

rewritecond %{REQUEST_URI}!^/folder1/  
rewritecond %{REQUEST_URI}!^/folder2/  
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1  

编辑排除特定文件夹:链接到手册

You could exclude specific folder via

rewritecond %{REQUEST_URI}!^/folder1/  
rewritecond %{REQUEST_URI}!^/folder2/  
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1  

Edit: Link to the manual

筱武穆 2024-08-05 20:13:58

只需再做一次检查:

if (preg_match('/^[^\\/.]+$/', $page)) {
    $page .= '/index';
}

如果 $page 只是 subfolder,则 /index 将被附加到 subfolder/index

Just do another check:

if (preg_match('/^[^\\/.]+$/', $page)) {
    $page .= '/index';
}

If $page is just subfolder, /index would be appended to have subfolder/index.

兔小萌 2024-08-05 20:13:56

这样,您就不必定义任何目录名称 - 它会将它们全部排除在外。

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1

不过,您可能需要测试尾部斜杠,它可能适用于 /subfolder/ 但不适用于 /subfolder

With this, you shouldn't have to define any directory names - it rules them all out.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1

You may need to test the trailing slash though, it may work on /subfolder/ but not /subfolder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文