最小化 htaccess 代码

发布于 2024-11-06 20:24:47 字数 553 浏览 2 评论 0原文

RewriteCond %{THE_REQUEST} \?event_id=154
RewriteRule ^events/(index.php)$ www.xyz.com/amit2011/?  [R=301,L]

RewriteCond %{THE_REQUEST} \?event_id=156
RewriteRule ^events/(index.php)$ www.xyz.com/munjal2011/?  [R=301,L]

RewriteCond %{THE_REQUEST} \?event_id=157
RewriteRule ^events/(index.php)$ www.xyz.com/smayra2011/?  [R=301,L]

RewriteCond %{THE_REQUEST} \?event_id=158
RewriteRule ^events/(index.php)$ www.xyz.com/vandna2011/?  [R=301,L]

我还有很多这样的基于 event_id 的重写规则。随着 event_id 的增加,重写规则将按比例增加。我们能否合并以上这些规则,以便我可以最小化 htaccess 的代码。

RewriteCond %{THE_REQUEST} \?event_id=154
RewriteRule ^events/(index.php)$ www.xyz.com/amit2011/?  [R=301,L]

RewriteCond %{THE_REQUEST} \?event_id=156
RewriteRule ^events/(index.php)$ www.xyz.com/munjal2011/?  [R=301,L]

RewriteCond %{THE_REQUEST} \?event_id=157
RewriteRule ^events/(index.php)$ www.xyz.com/smayra2011/?  [R=301,L]

RewriteCond %{THE_REQUEST} \?event_id=158
RewriteRule ^events/(index.php)$ www.xyz.com/vandna2011/?  [R=301,L]

I have many more rewrite rules like this based on event_id. As the event_id increases rewrite rules will be increased propotionaly. Can we merge these above rules, so that i can minimized the code of htaccess.

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

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

发布评论

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

评论(1

迷荒 2024-11-13 20:24:47

我相信您可以使用 RewriteMap 来实现这一点。但这或多或少只是将规则与数字->路径名映射分开。

RewriteMap idmap txt:../../idmap.txt

RewriteCond %{THE_REQUEST} \?event_id=(\d+)
RewriteRule ^events/(index.php)$ www.xyz.com/${idmap:%1}/?  [R=301,L]

单独的 idmap.txt 只会列出:

154  amit2011
156  munjal201
157  smayra2011
158  vandna2011

它是一种简单的文本格式,可以自动生成。

<小时>

正如其他评论中所指出的,在 PHP 脚本中实现这一点也非常简单,而不是仅使用 .htaccess 中的 RewriteRules。 -- 只需将所有规则替换为:

RewriteCond %{THE_REQUEST} \?event_id=(\d+)
RewriteRule ^events/(index.php)$ rewrite.php?id=%1  [L]

并创建一个 rewrite.php 脚本:

<?php
   $map = array(
      154 => "amit2011",
      156 => "munjal2011",
      157 => "smayra2011",
      158 => "vandna2011",
   );
   header("Location: http://www.xyz.com/{$map[$_GET['id']]}");
   header("Status: 301 Redirect");

I believe you could use a RewriteMap for that. But this more or less just separates the rule from the number->pathname map.

RewriteMap idmap txt:../../idmap.txt

RewriteCond %{THE_REQUEST} \?event_id=(\d+)
RewriteRule ^events/(index.php)$ www.xyz.com/${idmap:%1}/?  [R=301,L]

And the separate idmap.txt would just list:

154  amit2011
156  munjal201
157  smayra2011
158  vandna2011

It's a simple textual format, and could be auto-generated.


As pointed out in the other comments, this is also quite simple to implement in a PHP script rather than with just RewriteRules in .htaccess. -- Just replace all your rules with:

RewriteCond %{THE_REQUEST} \?event_id=(\d+)
RewriteRule ^events/(index.php)$ rewrite.php?id=%1  [L]

And create a rewrite.php script instead:

<?php
   $map = array(
      154 => "amit2011",
      156 => "munjal2011",
      157 => "smayra2011",
      158 => "vandna2011",
   );
   header("Location: http://www.xyz.com/{$map[$_GET['id']]}");
   header("Status: 301 Redirect");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文