有没有办法在 .htaccess 中执行某种正则表达式循环

发布于 2024-09-16 04:02:00 字数 389 浏览 4 评论 0原文

您好,目前我的 .htaccess 文件中有一个规则列表,但如果我需要添加新页面,则需要再次编辑此文件并添加另一个规则。这里有一些

RewriteRule ^admin/(.*).html$ index.php?x=admin&y=$1
RewriteRule ^admin/project/(.*).html$ index.php?x=work&p=project&$1
RewriteRule ^work/(.*).html$ index.php?x=work&p=$1

有没有什么方法可以让我拥有一条适用于所有人的规则。有没有办法进行循环,以便对于 url 中的每个附加 /something/extra ,它都会将其添加到重定向中?

谢谢。

hi at the minute I have a list of rules in my .htaccess file but if I need to add a new page i then need to edit this file again and add yet another rulles. HEre is a few

RewriteRule ^admin/(.*).html$ index.php?x=admin&y=$1
RewriteRule ^admin/project/(.*).html$ index.php?x=work&p=project&$1
RewriteRule ^work/(.*).html$ index.php?x=work&p=$1

Is there some way that I can have 1 rule that will work for all. Is ther ways to do loops so that for each additional /something/extra in the url it will add it in the redirect??

Thanks.

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

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

发布评论

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

评论(1

我不在是我 2024-09-23 04:02:00

简单的答案是否定的,不可能创建可以处理动态数量的变量的 RewriteRule。您唯一真正的选择是捕获基础知识,然后将所有内容捕获为单个块并使用 PHP 对其进行解析。也许可以改进您当前所拥有的内容,这取决于第二条规则中的 x=work 是否是拼写错误,以及您的查询字符串变量的一致性......

RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.html$ index.php?x=$1&y=$2

这是您可能在基于MVC的系统。但它们通常具有路由系统,允许您指定要匹配的模式(这与必须以多种方式添加 RewriteRules 相同)。

The simple answer is no it is not possible to create a RewriteRule that can cope with a dynamic amount of variables. Your only real option is to capture the basics and capture everything afterwards as a single chunk and parse it with PHP. It may be possible to improve on what you currently have there, it depends whether the x=work in your second rule is a typo, and how consistent your query string variables are...

RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.html$ index.php?x=$1&y=$2

This is the sort of pattern you might see in an MVC based system. But they usually have routing system to allow you to specify patterns to match (which is the same as having to add the RewriteRules in many ways).

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