We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Quote from: Yacoby at Nov 03, 2009, 01:01 PM

    Quote from: bunk58 at Nov 02, 2009, 02:13 PM

    Thanks for the info, I’m going tot try it on a site where I was getting the 301 loop.
    LMK if it works. If it does I would suggest that the line is changed to something like this:
    ($parts[0] != $strictURL && $requestedURL != $strictURL)


    Or a better fix if someone can be bothered to diagnose the full problem (which I think could be due to the fact that makeUrl possibly returns the full url rather than a relative url).
    make->URL returns a relative URL by default, but takes four arguments that give you a lot of control over the resulting URL:

    http://wiki.modxcms.com/index.php/API:makeUrl
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 16182
      • 56 Posts
      The plugin didn’t work for me for the startpage. The page http://www.example.com/?id=1 never got redirected. But ?id=2 etc... works well. I fixed it by changing a line in the code to
               if ($requestedURL != $modx->config['site_url'] || $parts[1] == "id=".$modx->config['site_start'])
      
      • I have some custom URL rewriting going on, so that one MODx document handles output for a custom data layer (products).
        I encountered a problem in this scenario. I set the override TV on my MODx controller resource to "Disabled", yet the URL was still being rewritten.

        Here’s an example:
        I have a MODx Resource container:
        http://mysite.com/products/spacestations/

        I want products in that category to have their own URLs, but not ones that map to MODx Resources, so that
        http://mysite.com/products/spacestations/alpha1.html
        would be rewritten to
        http://mysite.com/product_controller.html?cat=53&prod=101

        So again, even with the override TV set to "Disabled", my rewrite rule triggers SEO Strict URLs to redirect to the MODx resource, instead of allowing it to be hidden behind the rewrite.

        It seems that the "Disabled" function doesn’t work in this scenario, and I’ve modified the plugin code so that it does.
        All better now, but I’m not sure if this will cause any troubles in other "Disabled" scenarios, as per how the plugin authors intended.

        //<?php
        // Strict URLs
        // version 1.0.1
        // Enforces the use of strict URLs to prevent duplicate content.
        // By Jeremy Luebke @ www.xuru.com
        // Contributions by Brian Stanback @ www.stanback.net
        
        // On Install: Check the "OnWebPageInit" & "OnWebPagePrerender" boxes in the System Events tab.
        // Plugin configuration: &editDocLinks=Edit document links;int;1 &makeFolders=Rewrite containers as folders;int;1 &emptyFolders=Check for empty container when rewriting;int;0 &override=Enable manual overrides;int;0 &overrideTV=Override TV name;string;seoOverride
        
        // For overriding documents, create a new template variabe (TV) named seoOverride with the following options:
        //    Input Type: DropDown List Menu
        //    Input Option Values: Disabled==-1||Base Name==0||Append Extension==1||Folder==2
        //    Default Value: -1
        
        //  # Include the following in your .htaccess file
        //  # Replace "example.com" &  "example\.com" with your domain info
        //  RewriteCond %{HTTP_HOST} .
        //  RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
        //  RewriteRule (.*) http://www.example.com/$1 [R=301,L] 
        
        // Begin plugin code
        $e = &$modx->event;
        
        if ($e->name == 'OnWebPageInit') {
            $documentIdentifier = $modx->documentIdentifier;
            if ($documentIdentifier)  // Check for 404 error
            {
                $myProtocol = ($_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
                $s = $_SERVER['REQUEST_URI'];
                $parts = explode("?", $s);
        
                $alias = $modx->aliasListing[$documentIdentifier]['alias'];
                if ($makeFolders)
                {
                    if ($emptyFolders)
                    {
                        $result = $modx->db->select('isfolder', $modx->getFullTableName('site_content'), 'id = ' . $documentIdentifier);
                        $isfolder = $modx->db->getValue($result);
                    }
                    else
                    {
                        $isfolder = (count($modx->getChildIds($documentIdentifier, 1)) > 0) ? 1 : 0;
                    }
                }
        
                if ($override && $overrideOption = $modx->getTemplateVarOutput($overrideTV, $documentIdentifier))
                {
                    switch ($overrideOption[$overrideTV])
                    {
                        case -1:
                            $isdisabled = 1;
                            break;
                        case 0:
                            $isoverride = 1;
                            break;
                        case 1:
                            $isfolder = 0;
                            break;
                        case 2:
                            $makeFolders = 1;
                            $isfolder = 1;
                    }
                }
        
                if(!$isdisabled) {
                    if ($isoverride)
                    {
                        $strictURL = preg_replace('/[^\/]+$/', $alias, $modx->makeUrl($documentIdentifier));
                    }
                    elseif ($isfolder && $makeFolders)
                    {
                        $strictURL = preg_replace('/[^\/]+$/', $alias, $modx->makeUrl($documentIdentifier)) . "/";
                    }
                    else
                    {
                        $strictURL = $modx->makeUrl($documentIdentifier);
                    }
        
                    $myDomain = $myProtocol . "://" . $_SERVER['HTTP_HOST'];
                    $newURL = $myDomain . $strictURL;
                    $requestedURL = $myDomain . $parts[0];
        
                    if ($documentIdentifier == $modx->config['site_start'])
                    {
                        if ($requestedURL != $modx->config['site_url'])
                        {
                            // Force redirect of site start
                            header("HTTP/1.1 301 Moved Permanently");
                            $qstring = preg_replace("#(^|&)(q|id)=[^&]+#", '', $parts[1]);  // Strip conflicting id/q from query string
                            if ($qstring) header('Location: ' . $modx->config['site_url'] . '?' . $qstring);
                            else header('Location: ' . $modx->config['site_url']);
                            exit(0);
                        }
                    }
                    elseif ($parts[0] != $strictURL)
                    {
                        // Force page redirect
                        header("HTTP/1.1 301 Moved Permanently");
                        $qstring = preg_replace("#(^|&)(q|id)=[^&]+#", '', $parts[1]);  // Strip conflicting id/q from query string
                        if ($qstring) header('Location: ' . $strictURL . '?' . $qstring);
                        else header('Location: ' . $strictURL);
                        exit(0);
                    }
                }
            }
        }
        elseif ($e->name == 'OnWebPagePrerender')
        {
            if ($editDocLinks)
            {
                $myDomain = $_SERVER['HTTP_HOST'];
                $furlSuffix = $modx->config['friendly_url_suffix'];
                $baseUrl = $modx->config['base_url'];
                $o = &$modx->documentOutput; // get a reference of the output
        
                // Reduce site start to base url
                $overrideAlias = $modx->aliasListing[$modx->config['site_start']]['alias'];
                $overridePath = $modx->aliasListing[$modx->config['site_start']]['path'];
                $o = preg_replace("#((href|action)=\"|$myDomain)($baseUrl)?($overridePath/)?$overrideAlias$furlSuffix#", '${1}' . $baseUrl, $o);
        
                if ($override)
                {
                    // Replace manual override links
                    $sql = "SELECT tvc.contentid as id, tvc.value as value FROM " . $modx->getFullTableName('site_tmplvars') . " tv ";
                    $sql .= "INNER JOIN " . $modx->getFullTableName('site_tmplvar_templates') . " tvtpl ON tvtpl.tmplvarid = tv.id ";
                    $sql .= "LEFT JOIN " . $modx->getFullTableName('site_tmplvar_contentvalues') . " tvc ON tvc.tmplvarid = tv.id ";
                    $sql .= "LEFT JOIN " . $modx->getFullTableName('site_content') . " sc ON sc.id = tvc.contentid ";
                    $sql .= "WHERE sc.published = 1 AND tvtpl.templateid = sc.template AND tv.name = '$overrideTV'";
                    $results = $modx->dbQuery($sql);
                    while ($row = $modx->fetchRow($results))
                    {
                        $overrideAlias = $modx->aliasListing[$row['id']]['alias'];
                        $overridePath = $modx->aliasListing[$row['id']]['path'];
                        switch ($row['value'])
                        {
                            case 0:
                                $o = preg_replace("#((href|action)=\"($baseUrl)?($overridePath/)?|$myDomain$baseUrl$overridePath/?)$overrideAlias$furlSuffix#", '${1}' . $overrideAlias, $o);
                                break;
                            case 2:
                                $o = preg_replace("#((href|action)=\"($baseUrl)?($overridePath/)?|$myDomain$baseUrl$overridePath/?)$overrideAlias$furlSuffix/?#", '${1}' . rtrim($overrideAlias, '/') . '/', $o);
                                break;
                        }
                    }
                }
        
                if ($makeFolders)
                {
                    if ($emptyFolders)
                    {
                        // Populate isfolder array
                        $isfolder_arr = array();
                        $result = $modx->db->select('id', $modx->getFullTableName('site_content'), 'published > 0 AND isfolder > 0');
                        while ($row = $modx->db->getRow($result))
                            $isfolder_arr[$row['id']] = true;
                    }
        
                    // Replace container links
                    foreach ($modx->documentListing as $id)
                    {
                        if ((is_array($isfolder_arr) && isset($isfolder_arr[$id])) || count($modx->getChildIds($id, 1)))
                        {
                            $overrideAlias = $modx->aliasListing[$id]['alias'];
                            $overridePath = $modx->aliasListing[$id]['path'];
                            $o = preg_replace("#((href|action)=\"($baseUrl)?($overridePath/)?|$myDomain$baseUrl$overridePath/?)$overrideAlias$furlSuffix/?#", '${1}' . rtrim($overrideAlias, '/') . '/', $o);
                        }
                    }
                }
            }
        }
        


        Diff is here http://pastebin.com/pastebin.php?diff=f6dbbcaa7
        Note, the plugin had a mix of tabs and spaces; I’ve cleaned it up to indents = 4 spaces.
          Mike Schell
          Lead Developer, MODX Cloud
          Email: [email protected]
          GitHub: https://github.com/netProphET/
          Twitter: @mkschell
          • 18463
          • 121 Posts
          Martijn van Turnhout Reply #84, 13 years, 9 months ago
          I got another problem with "SEO Strict URLS". I’ve put a MaxiGallery snippet on the homepage of a website I’m currently designing.

          This is the code of the snippet:

          [!MaxiGallery? &display=`embedded` &embedtype=`slimbox` &lang=`nl` &pics_per_row=`4` &pics_per_page=`40` &max_thumb_size=`137` &max_pic_size=`800`!]

          I’ve enabled SEO Strict URLS, so even when people are typing www.mywebsite.com/index.php, they get redirected to www.mywebsite.com. So, when I’m logged into the CMS and I’m trying to "manage pictures" at the front-end of the homepage, I get nothing. Nada.

          Now, I know what the problem is: I’ve disabled "SEO Strict URLS" and when I click on "Manage pictures" then, it all works fine and I get redirected to www.mywebsite.com/home/, where I can edit all the pictures. But when the SEO plugin is turned on, I get nothing.

          Is it possible for me to disable the SEO Strict URLs plugin for just the homepage or is there any other way to fix this problem?
            • 5246
            • 12 Posts
            Hey there,

            after installation I am still able to access my site via:

            www.example.com/home
            www.example.com/home.html
            www.example.com/index.php?id=1
            ...

            There is no rewriting at all (apart from the directives in the .htaccess), which would mean that the plugin points me to

            www.example.com/home.html

            like I think it is supposed to...
              • 5253
              • 17 Posts
              hey Chris,

              have you checked the correct system events in the Event-Tab?
              You have to check these two:
              OnWebPageInit
              OnWebPagePrerender


              @all:
              If you’re using "/" as FURL suffix you’ll get problems by using a ressource alias like "sitemap.xml".
              StrictUrls will redirect this to example.com/sitemap.xml/

              I found a (dirty) solution by editing these lines:
                    if ($isoverride)
                    {
              		 $strictURL = preg_replace('/[^\/]+$/', $alias, $modx->makeUrl($documentIdentifier));
              		 if (strpos($alias, '.') !== false){
              			$strictURL = substr(preg_replace('/[^\/]+$/', $alias, $modx->makeUrl($documentIdentifier)), 0, -1);
              		 }	
                    }
              
              


              If anyone has an idea how to solve this problem in other ways, please let me know...

              Cheers,
              Chris
                *there are no strangers - just friends who never met*
                • 5246
                • 12 Posts
                Hey Chris,

                it’s Chris.

                I forgot to subscribe to this that’s why I reply so late.
                Yes, I followed several tutorials and I checked both.

                Today it revealed that we experienced some problems with W3C Validator. It stated (as other bots did, too) that a Internal Server Error 500 occured. We tried everything and found out that Strict SEO is the reason. We disabled it in consequence and everything works fine now. Nevertheless, I want to find out what is the problem, but didn’t have the time so far. I think it is some kind of misconfiguration on our end. My first suspicion is that a line in .htaccess is trying to override some server config that cannot be changed due to lack of rights... Will check this...
                  • 21259
                  • 1 Posts
                  Quote from: ApoXX at Feb 24, 2007, 11:33 PM

                  By the way, to enforce the www on URLs, users can use the following mod_rewrite rule with the Apache webserver:

                  # Include the following in your .htaccess file
                  # Replace "example.com" &  "example\.com" with your domain info
                  RewriteCond %{HTTP_HOST} .
                  RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
                  RewriteRule (.*) http://www.example.com/$1 [R=301,L]
                  


                  To enforce URLs without the www, simply remove all references of www. in the htaccess rule:

                  # Include the following in your .htaccess file
                  # Replace "example.com" &  "example\.com" with your domain info
                  RewriteCond %{HTTP_HOST} .
                  RewriteCond %{HTTP_HOST} !^example\.com [NC]
                  RewriteRule (.*) http://example.com/$1 [R=301,L]
                  


                  Thank you for this info & links. I read about duplicate content (which seems to be google’s biggest bug up their...) and never realized having the same content with and without www could be trouble. Rather shocked Big G can’t differentiate between the two url’s. I will definitely use the code you provided.

                  Cathy
                  • Version 1.0.5 beta