Mod Rewrite rule

Hello!

I want to 301 all my feeds to their new URLs.

How can I rewrite this:

mysite.com/rss.php?atom&f=5&t=1

to

mysite.com/feed.php?atom&f=5&mode=topics

This is dynamic. All URL parameters are optional, and “f” can be any number. (“t=1” will always be “mode=topics”)

Thanks!

For redirects like this it’s probably easier to keep rss.php but change the code to inspect the GET parameters, build the new query string, and redirect via the PHP. e.g.


<?php
$get=array();
if (isset($_GET['f']) && ctype_digit($_GET['f'])) {
  $get[] = 'f='.$_GET['f'];
}
if (isset($_GET['t']) && $_GET['t'] == 1) {
  $get[] = 't=topics';
}
header('Location: http://'.$_SERVER['HTTP_HOST'].'/feed.php?atom&' . implode('&', $get));
exit();

Simply because it’s a lot easier to handle those optional parameters in PHP than it would be in Apache.

Thanks. A few days ago I came up with this and it seems to work, do you see any issues with it.


RewriteCond %{QUERY_STRING} ^(.*)t=1$
RewriteRule ^rss.php http://www.mysite.com/feed.php?%1mode=topics [L,R=301]
RewriteRule ^rss.php http://www.mysite.com/feed.php [L,R=301]

I would change it slightly, but just slightly


RewriteCond %{QUERY_STRING} ^(.*)t=1$
RewriteRule ^rss[COLOR="Red"]\\[/COLOR].php[COLOR="Red"]$[/COLOR] http://www.mysite.com/feed.php?%1mode=topics [L,R=301]

RewriteRule ^rss[COLOR="Red"]\\[/COLOR].php[COLOR="Red"]$[/COLOR] http://www.mysite.com/feed.php [L,R=301]

That is

1) Put a \ in front of the dot since it is a regular expression where a dot means “any character”, but you want to match it literally so you need to prepend a \
The rule works fine without it (since a dot also falls under “any character”), but it’s not semantically correct.

2) Put a $ at the end of the regex to indicate the end of the string. Just for specifity, so it doesn’t match e.g. rss.php3

3) Separate the second RewriteRule from the first RewriteRule and RewriteCond with a blank line to indicate they are not related. For these three rules it doesn’t really matter, but as your .htaccess gets bigger it gets harder and harder to keep a good overview; keeping related stuff together really helps! :slight_smile:
This might be just personal preference of course.

Thanks for the corrections :slight_smile: