我的 .htaccess 中的递归循环

发布于 2024-09-07 21:19:03 字数 691 浏览 1 评论 0原文

我在向我的 apache .htaccess 添加默认重写 php 和 html 请求时遇到问题。

这个想法是为我的网站上的内容设置模板,指定文件夹中的内容使用特定的模板。它适用于 html、图像和其他资源;请求被很好地重写到模板中。但是我在重写对 php 资源的请求时遇到了递归问题,因为模板也是一个 php 资源……也许逻辑上足够了。

是否可以将 php 到 php 限制为一次重写?或者我需要另一种方法吗?

这条规则是我的问题:

RewriteRule ^(.*).(php$) /_templates/default.php?$1.$2 [NC,L]

下面是我的.htaccess...

谢谢...

# Template rewrites:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(subpages/.*) /_templates/present.php?$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).html$ /_templates/default.php?$1 [NC,L]
RewriteRule ^(.*).(php$) /_templates/default.php?$1.$2 [NC,L]

I have a problem adding a default rewrite php and html requests to my apache .htaccess.

The idea is to set up templates for the content on my site, specifying that the content in a folder uses a specific template. It works fine for html, image and other resources; requests are rewritten to the template nicely. But I get a recursive problem rewriting request to php-resources, because the template is also a php-resource… perhaps logically enough.

Is it possible to limit the php-to-php to a single rewrite? Or do I need another approach ?

This rule is my problem:

RewriteRule ^(.*).(php$) /_templates/default.php?$1.$2 [NC,L]

Below is my .htaccess...

Thanks...

# Template rewrites:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(subpages/.*) /_templates/present.php?$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).html$ /_templates/default.php?$1 [NC,L]
RewriteRule ^(.*).(php$) /_templates/default.php?$1.$2 [NC,L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

彩扇题诗 2024-09-14 21:19:03

您可以像这样添加 RewriteCond:

RewriteCond $1 !^_templates/
RewriteRule ^(.*).php$ /_templates/default.php?$1.php [NC,L,B]

我还添加了 B 标志,因为您在查询字符串中使用了反向引用。

You can add a RewriteCond like this:

RewriteCond $1 !^_templates/
RewriteRule ^(.*).php$ /_templates/default.php?$1.php [NC,L,B]

I also added the B flag because you're using the backreference in the query string.

情独悲 2024-09-14 21:19:03

您可以添加排除规则:

RewriteRule /_templates/default.php(.*)$ $0 [L]

$0 是原始请求,[L] 表示这是此请求将运行的最后一条规则

You can add an exclusion rule :

RewriteRule /_templates/default.php(.*)$ $0 [L]

$0 is the original request and [L] means it is the last rule this request will run

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