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的增加,重写规则将会增加propotionaly。我们可以合并上述规则,以便最小化htaccess的代码。
答案 0 :(得分:4)
我相信你可以使用RewriteMap。但这或多或少只是将规则与number->路径名映射分开。
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");