We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36620
    • 65 Posts
    By default '-' is inserted between words in the URL alias, which file contains the code for this as I would prefer it to automatically default to underscore instead? e.g.

    modx_using_underscore_by_default
    [ed. note: randall last edited this post 14 years, 11 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Underscores have been considered highly undesirable for some time now because the underscores are not visible in the address bar of many browsers, being hidden by the bottom line of the address bar.

      There is an event 'OnStripAlias' which allows you to process the alias in a plugin; if there is such a plugin then the alias becomes the output of the plugin, otherwise the dash is inserted. Personally, I would simply copy the code for the default behavior, and change the dashes in the regular expressions to underscores.

      	/**
      	 * Format alias to be URL-safe. Strip invalid characters.
      	 *
      	 * @param string Alias to be formatted
      	 * @return string Safe alias
      	 */
          function stripAlias($alias) {
              // let add-ons overwrite the default behavior
              $results = $this->invokeEvent('OnStripAlias', array ('alias'=>$alias));
              if (!empty($results)) {
                  // if multiple plugins are registered, only the last one is used
                  return end($results);
              } else {
                  // default behavior: strip invalid characters and replace spaces with dashes.
                  $alias = strip_tags($alias); // strip HTML
                  $alias = preg_replace('/[^\.A-Za-z0-9 _-]/', '', $alias); // strip non-alphanumeric characters
                  $alias = preg_replace('/\s+/', '-', $alias); // convert white-space to dash
                  $alias = preg_replace('/-+/', '-', $alias);  // convert multiple dashes to one
                  $alias = trim($alias, '-'); // trim excess
                  return $alias;
              }
          }
          
      

        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 36620
        • 65 Posts
        Thank you!

        Good point, unfortunately the site is fairly established and I don't want to lose its Google ranking by changing it to hashes.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          To avoid confusion, you should be aware that a hash is #, also called a pound sign and more properly an octothorpe; what you are talking about is a dash - also called a hyphen.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 36620
            • 65 Posts
            Ok, corrected subject title.