We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18367
    • 834 Posts
    This could easily go under SEO but right now it’s a navigation issue for me.

    OK,

    so hyphens are better than underscores in terms of seo friendly urls.

    Unfortunately my existing website has underscores don’t ask me why it was built a long time ago.

    According to Ask Apache I can use the following code to set the 301 redirects

    The Apache .htaccess underscore to hyphen conversion code


    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    RewriteRule !\.(html|php)$ - [S=5]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
    RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]

    RewriteCond %{ENV:underscores} ^Yes$
    RewriteRule (.*) http://your domain/$1 [R=301,L]

    I’ll try it ourt and see how it goes.

    Anyone else done this before? grin


    Hmmm

    Tried it without any luck.

    Any Apache guru’s out there?


      Content Creator and Copywriter
      • 18367
      • 834 Posts
      OK, got it working, but with a few riders.

      If anyone’s interested ask and I’ll post the script.

      However it does disable a few admin features, probably because of the global nature of the rewrite rule, but hey I’m no expert.
        Content Creator and Copywriter
        • 7231
        • 4,205 Posts
        Glad you got it working, but it seems scary that you had to give up some functions. I am not sure I understand the original issue; Your site (old site) uses underscore in the url and the new site uses the hyphen. And the issue is to have the old url (with _) be rewriten to the new url (with -).

        You could use the Virtual Aliases plugin to have multiple aliases for each document.

        Another option would be to simply stick to the _ and edit the auto generated aliases in modx to use the _ rather than the -.

        There are a few other solutions for multiple aliases in the forum but I do not have the bookmarks on hand.
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don't know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 18367
          • 834 Posts
          dev,

          yes you do understand the original issue. Apparently it’s quite a common occurrence as there are a number of apache blogs explaining how to rectify it.

          It seems the mod_rewrite option is the favoured one.

          the hyphen is preferred by SEO guns as it allows robots to read the url as a collection of words. Apparently the underscore means the url gets treated as one long word. (At least that’s my understanding or it.)

          I wasn’t prepared to lose the admin functionality in the short term so i reverted back to the old file, and posted a few questions on some of the apache blogs to see if they could tell me where iwas going wrong.

          As for the code: My original .htaccess file reads:

          
          RewriteEngine On
          RewriteBase /"mydomain name"/
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ /mydomainname/index.php?q=$1 [L,QSA]


          And that all works ok.

          The modified code reads:

          Options +FollowSymLinks
          RewriteEngine on
          RewriteBase /mydomain/
          RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ /acwa/$1-$2-$3-$4-$5 [R=301,L]
          RewriteRule ^([^_]*)_([^_]*)_(.*)$ /acwa/$1-$2-$3 [R=301,L]
          RewriteRule ^([^_]*)_(.*)$ /mydomain/$1-$2 [R=301,L]
          
          RewriteEngine On
          RewriteBase /mydomain/
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ /mydomain/index.php?q=$1 [L,QSA]


          As far as rewriting the underscores to hyphens it works.

          However there are some side effects:

          1: It mangles the design view in Quick Edit, although it’s fine in normal browsing.

          2: In the backend TinyMCE disappears from the document editing pane.

          3: Some of the sorting icons disappears from the left admin tree menu.

          It’s not permanent, as if I revert back tot he old .htaccess file everything goes back to normal. [We do all remember to create backups of files before we mess with them don’t we?]

          Anyway I’m guessing the global nature of the rewrite rule is affecting other parts of Modx.

          Apparently the more specific you can make the rewrite rule the better. In this case specify what file type extension the rule should apply to.

          As I said I’m no expert, any guns out there please feel free to point out the error of my ways.



            Content Creator and Copywriter
          • As far as the Manager goes, you did rename the ht.access file in the /manager folder to .htaccess? It turns off the rewrite engine altogether.

            I also don’t think you should have two lines turning the engine on.
              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
              • 4310
              • 2,310 Posts
              Options +FollowSymLinks
              RewriteEngine on
              RewriteBase /mydomain/
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteRule ^(.*)$ /mydomain/index.php?q=$1 [L,QSA]
              RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ /acwa/$1-$2-$3-$4-$5 [R=301,L]
              RewriteRule ^([^_]*)_([^_]*)_(.*)$ /acwa/$1-$2-$3 [R=301,L]
              RewriteRule ^([^_]*)_(.*)$ /mydomain/$1-$2 [R=301,L]

              Have you tried it like this?
              I found that anything before the basic standard rewrite rules messed things up.
                • 18367
                • 834 Posts
                bunk,

                it’s weird, this fixes the side effects,

                but then rewrite no longer works.

                  Content Creator and Copywriter