如何使用 .htaccess 进行多个重写规则?

发布于 2024-10-21 12:00:26 字数 305 浏览 2 评论 0原文

我正在尝试使用 Drupal 建立一个多语言网站。

我喜欢使用以下网址格式

http://domain/[language]/[node id]

因此我将以下规则添加到.htaccess以进行测试

RewriteRule ^jpn/[0 -9]$ jpn.html

问题是规则被下面的规则覆盖

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

怎么办我有多个重写规则吗?

I am trying to build a multilingual website with Drupal.

I like to have the following url format

http://domain/[language]/[node id]

so I added the following rule to .htaccess for testing purpose

RewriteRule ^jpn/[0-9]$ jpn.html

The problem is that the rule is overwritten by the following rule

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

How do I have multiple rewrite rules?

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

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

发布评论

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

评论(2

薯片软お妹 2024-10-28 12:00:26

您的第二个 RewriteRule 具有 L 标志设置,这意味着如果规则匹配,则不会处理进一步的规则。

如果您希望第一条规则也停止任何进一步的处理,请也向其中添加 L 标志。

RewriteRule ^jpn/[0-9]$ jpn.html [L]

还要确保您的第二条规则列在最后,因为它与所有内容 (.*) 匹配,因此 Apache 在它之后将永远不会看到任何其他规则。

编辑:L 旗 URL

Your second RewriteRule has the L Flag set, which means that if the rule matches, no further rules will be processed.

If you want your first rule to also stop any further processing, add the L Flag to it as well.

RewriteRule ^jpn/[0-9]$ jpn.html [L]

Also make sure that your second rule is listed last, because it matches everything (.*) and thus, Apache will never see any other rule after it.

Edited: the L Flag URL

从﹋此江山别 2024-10-28 12:00:26

我不确定这个 [L] 在每种情况下是否真的有效,以避免相互覆盖。我不是这方面的专家,但我花了一天的时间才发现,你只需要向之前有效的 RewriteRule 添加 1 个 Atome,而不会被覆盖,并且它会从第一个开始再次被覆盖。我很难相信 1 个 htaccess 中的 2 个不同的规则可以正确地适用于 2 个不同的文件。

两者均 100% 正常

RewriteRule ([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ display.php?$1&category=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$  news.php?$1 [L]

工作

news

Link 2 显示的是 link1 的结果...,请告诉我为什么?

RewriteRule ([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ display.php?$1&category=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ news.php?$1&obj=$2&search=$3 [L]

显示

消息

I'm not sure this [L] really works in each case to avoid overwrite one each other. I'm not an expert on this, but i spent a day to figure out that you just need to add 1 Atome to a RewriteRule that did worked before without beeing overwriten, and it starts to get overwriten again from the first one. It's hard for me to believe that 2 different Rules, in 1 htaccess can properly work for 2 differents files.

Both Worked 100% well

RewriteRule ([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ display.php?$1&category=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$  news.php?$1 [L]

displ

news

Link 2 displays the result of link1 instead..., just tell me why?

RewriteRule ([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ display.php?$1&category=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ news.php?$1&obj=$2&search=$3 [L]

displ

news

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