So since e107 had links like “http://iandouglas.com/page?3.0″ for my old SpamAssassin trainer tutorial page, I decided I should at least attempt to toss in a few mod_rewrite rules to even up the playing field so search engines and people with bookmarks could still get to the tutorial text which I’ve started adding to the new CMS.
Trouble is, mod_rewrite doesn’t support something simple like:RewriteRule ^page.php?3.0$ /spamassassin-trainer/ [R=301,L]
It’s not supported because the “?” is a reserved regular expression (regexp) character meaning “whether or not the preceeding bits existed”. For example:RewriteRule ^abc?def.html$ ...
… would match “abcdef.html” and “def.html” because the “?” means “whether or not ‘abc’ was part of the filename”.
So for my use, where my old URLs look like “page.php?3.2″ (my biggest beef with e107 is non-SEO friendly URLs), I needed to find a way to tackle the “?” issue.
I tried escaping the question mark, but that didn’t work:RewriteRule ^page.php\?3.0$ /spamassassin-trainer/ [R=301,L]
I tried putting (.) in place, which tells the regexp engine to check for the existence of any character, but that didn’t work:RewriteRule ^page.php(.)3.0$ /spamassassin-trainer/ [R=301,L]
In the end, since the SilverStripe CMS doesn’t include a page called page.php, I just created my own, and it looks like this (edited for brevity):$querystring = $_SERVER['QUERY_STRING'] ;
$newpage = "http://iandouglas.com/news/" ;
switch($querystring) {
case "3" :
case "3.0" :
$newpage = "/spamassassin-trainer/" ;
break ;
case "3.1" :
$newpage = "/sa-trainer-assumptions-and-terminology/" ;
break ;
}
header("HTTP/1.1 301 Moved Permanently") ;
header("Location: ".$newpage) ;
?>
The key here was just looking at the existing query string coming in, and doing a switch() check on it, and building a new URL for each match. Then, to cover the [R=301] for the “permanently moved” redirect for mod_rewrite, you simply have to do a header() acll in PHP with the HTTP 1.1 string before the header() call to bounce the browser to the new page.
So far, this works really well. I’m debating doing something similar for my old blog posts until I get them all moved, but in the meantime a mod_rewrite redirect for the news.php script works just fine to jump to iandouglas.com/oldsite/news.php.
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
You must be logged in to post a comment.