0
votes
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.

1
Why don't you do that in php?zerkms
I would leave .htaccess as a static set of rules and do all dynamic work in PHP.binaryLV
@binary, Can you please tell me how can i do this in PHP?munjal
sorry, but I cannot provide any code, as I'm not too familiar with mod_rewrite. If it helps - I would redirect any \?event_id=(\d+) request to a single local PHP page/file which would map numerical ID to those "amit2011" etc and then redirect to "www.xyz.com/amit2011/" or something like that. Though, if you have an access to the server config and can configure VirtualHost, mario's solution might be better.binaryLV

1 Answers

4
votes

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