I personally don’t like to use .htaccess
I use PHP directly and in MODx, you can do that using a plugin
Here is the plugin code i use, you need to change the $correct_name to your site name that you prefer (www.yoursite.com or yoursite.com), I will stick with the one with www because everyone assume a site start with www. You also need to enable ’OnWebPageInit’ event for your plugin.
// <?
$correct_name = "yoursite.com"; // do not include http:// and trailing slash
$e = &$modx->Event;
switch ($e->name)
{
case "OnWebPageInit":
$q = $_GET['q'];
// canonical redirect
if ( $_SERVER['HTTP_HOST'] != $correct_name )
{
$url = "http://$correct_name/";
if ( $q != "index.php" )
$url .= $q;
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
exit;
}
default :
return;
break;
}
// ?>
Possible enhancements
1. Make use of MODx variable site_url
2. more checking to hide index.php (index.html, index.htm ...)
This is my little contribution before I am leaving my computer for the whole week!