ORANGE COUNTY, Calif. -- While working on the new website project, it became necessary to focus on maintaining content that was created in the legacy content management system. The system generated long alpha-numeric strings to retrieve content from the database. The path of the URL would be the same across a content type but the URL would also have a parameter string which represented the content in the database. Since there was no way to create a list of all the possible URLs it was critical to create a programmatic method to deal with this.
To create a 301 redirect with the .htaccess file that handle parameters in the URL use the following syntax:
RewriteCond %{QUERY_STRING} ^(.*)foobar=3411&foobar2=5&foobar3=2010$
RewriteRule ^path_arg_0/filename.php$ http://exampledomain.com/path_arg_0/Calendar.aspx?EventID=3411? [R=301,L]
The first statement include the parameters in the URL. The second statement include the path of the URL and location of the redirect.
For legacy systems that need to be retained even after a new website has launched, a great solution is moving the legacy site to a subdomain. With the live site at domain.com and the legacy site at legacy.domain.com you can implement a really easy 301 redirect method to cover all possible links to the legacy site. Since you just need to redirect to the subdomain, the path and parameters in the URL can stay the same.
For example if the link to the legacy site is domain.com/path0/path1/filename.php?param=example301 and you have moved the legacy site to legacy.domain.com you can implement a redirect that reacts when your server wants to give a 404 error message. In your .htaccess file add the following line:
ErrorDocument 404 /404.php
Now you can create a file called 404.php and place it in the root of your website. On 404.php include the following snippet of PHP code:
<?php
//This function grabs the URL from the browser address bar and returns it to you
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/*************************
* We want to redirect the browser to the new subdomain we have created.
* Since we have to determine if the user entered the site with a www in the URL,
* lets run the URL returned by curPageUrl() through a string replacement function
* that will either change out www or simply append 'legacy' to the host name.
* The new fully qualified URL will be stored in the $redirectTo variable.
*************************/
$redirectTo = str_replace("http://domain.com","http://legacy.domain.com",str_replace("www","legacy",curPageURL()));
// PHP Permanent 301 redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$redirectTo);
exit();
?>
So if you are like us, you are probably implementing a 301 redirect strategy with Drupal. As always Drupal makes things so much easier because of all the amazing people that contribute modules. The best way to implement the strategy we just talked about in Drupal is to use the Custom Errors module. From that module you can include custom PHP script that will execute when a "Page Not Found" error occurs. You will not need to modify the .htaccess file and editing the php code is done from the Drupal admin.