最小化 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]
我还有很多这样的基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您可以使用 RewriteMap 来实现这一点。但这或多或少只是将规则与数字->路径名映射分开。
单独的 idmap.txt 只会列出:
它是一种简单的文本格式,可以自动生成。
正如其他评论中所指出的,在 PHP 脚本中实现这一点也非常简单,而不是仅使用 .htaccess 中的 RewriteRules。 -- 只需将所有规则替换为:
并创建一个
rewrite.php
脚本:I believe you could use a RewriteMap for that. But this more or less just separates the rule from the number->pathname map.
And the separate
idmap.txt
would just list: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:
And create a
rewrite.php
script instead: