We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17544
    • 86 Posts
    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! laugh
      • 12652
      • 228 Posts
      That’s a nice edition, and great having an alternative.

      It would be interesting to know if one or the other is better as far as speed, less intensive on the server... or maybe they are equal in those regards.

      Either way, if it isn’t, that might be a nice one to get in the Repository.
        | Identity Developments delivers SEO focused web design and web presence services
        - it&#39;s not about websites, it&#39;s about your identity. |
        • 16885
        • 255 Posts
        Quote from: identity at Nov 25, 2006, 02:26 PM

        That’s a nice edition, and great having an alternative.

        It would be interesting to know if one or the other is better as far as speed, less intensive on the server... or maybe they are equal in those regards.

        Either way, if it isn’t, that might be a nice one to get in the Repository.
        .htaccess doesn’t use extra loading time/code for each page, the PHP code works on more than just Apache but is checked everytime you load the page...
          • 17544
          • 86 Posts
          Quote from: identity at Nov 25, 2006, 02:26 PM

          That’s a nice edition, and great having an alternative.

          It would be interesting to know if one or the other is better as far as speed, less intensive on the server... or maybe they are equal in those regards.

          Either way, if it isn’t, that might be a nice one to get in the Repository.

          Have been using the code for more than 4 years (before modx it is hardcoded into the header of my site). I prefer PHP because I am more comfortable with it then .htaccess. And yet, .htaccess is more simple (less lines).