[TYPO3] mod_rewrite - Mass Redirection Help Needed (I'm Desparate!)
Timothy Patterson
tjpatter at svsu.edu
Wed May 23 22:02:35 CEST 2007
We are moving away from a CMS that utilizes URLs in the form of
"http://www.domain.com/its/index.cfm?doc_id=10" in favor of Typo3.
I have RealURL working perfectly (great job devs!) and I am quite
satisfied with my setup.
My problem is the fact that I have to keep all of the old URLs valid on
my site. Due to the 1000s of pages on my site I would like to implement
a type of redirection database to keep the old URLs valid while
increasing manageability of the redirections.
After doing some research online, I have found that there is no
clean-cut way to redirect a URL in the style that our old CMS uses.
Example:
You can only redirect "http://www.domain.com/its/index.cfm" to any URL.
You cannot redirect "http://www.domain.com/its/index.cfm?doc_id=10" to
a URL and "http://www.domain.com/its/index.cfm?doc_id=20" to a different
URL. Catch my drift? "Standard" redirects only allow you to match file
names and not the parameters after the file name.
My solution was to create a MySQL database, then use a combination of
mod_rewrite with PHP.
If you view my sample .htaccess file and PHP script below, you can see
my problem... My setup works great except for the fact that I get a
?myredirect=0 appended to every single URL on my site. I've played with
mod_rewrite for hours to try to get rid of that, but it seems to be the
only way to avoid an infinite rewriting loop (it is the only way I can
communicate from the PHP script back to mod_rewrite). I really don't
want the ?myredirect=0 on every page.
Does anyone have any suggestions? Any better ways of accomplishing this
goal? Does anyone know of any other mass redirection solutions? Any
other hybrid PHP / mod_rewrite possibilities? Am I just crazy?
If I can get some help I'd be glad to write up a How-to doc that will
help others migrating away from a different CMS.
Here is what I have so far... (It works, just adds a ?myredirect=0 to
every page's url. Grr!)
mod_rewrite (in a .htaccess file):
RewriteEngine On
RewriteCond %{QUERY_STRING} !(myredirect)
RewriteRule .* /redirect.php?dbredirection=%{REQUEST_URI} [L,QSA]
And my PHP code:
<?php
// DB Connectivity Include...
include "config.php";
// Reads QUERY_STRING environment variable and grabs everything 'after
// dbredirection='
list($junk, $request) = split("dbredirection=", getenv('QUERY_STRING'));
// Replace the first instance of a & with a ? (caused by mod_rewrite)
$request = preg_replace("/(\&)/", "?", $request, 1);
// Connect to MySQL...
$connection = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db);
// Get today's date in MySQL format...
$expdate = date('Y-m-d');
// Build Query...
// Select & Check Expiration - Default is 2050-01-01 so we should be ok
// with this...
$query = "SELECT newurl FROM redirects WHERE oldurl='$request' AND
expires >= '$expdate' LIMIT 1";
// Execute Query...
$result = mysql_query($query);
if(mysql_num_rows($result) != 0)
{
// Retrieve Row...
$row = mysql_fetch_row($result);
//echo "Redirecting to: $row[0]";
//die;
// Perform Safe, "Permanent" Redirect...
header("HTTP/1.1 301 Moved Permanently");
header("Location: $row[0]");
die;
} else {
// No entries exist... Try to go to page...
// Add ?myredirect=0 to the URL to prevent mod_rewrite loops!!!
// Does $request contain a '?'
$qmark = strstr($request, '?');
if(!$qmark)
{
$request .= "?myredirect=0";
} else {
$request .= "&myredirect=0";
}
header("Location: $request");
die;
}
?>
More information about the TYPO3-english
mailing list