我的 .htaccess 中的递归循环
我在向我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样添加 RewriteCond:
我还添加了
B
标志,因为您在查询字符串中使用了反向引用。You can add a RewriteCond like this:
I also added the
B
flag because you're using the backreference in the query string.您可以添加排除规则:
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