开发者

Minimized the htaccess code

开发者 https://www.devze.com 2023-03-05 19:56 出处:网络
RewriteCond %{THE_REQUEST} \\?event_id=154 RewriteRule ^events/(index.php)$ www.xyz.com/amit2011/?[R=301,L]
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 thi开发者_开发百科s 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.


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号