We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19787
    • 28 Posts
    I am having issues setting up some mod_rewrite rules to hide extra query strings from my snippet. I have an extra query string called template that looks like the following :

    ?template=rss
    ?template=popular
    ?template=live

    i would like to do some rewrite rules that will alow those urls to be called like the following :

    /document-alias/rss
    /document-alias/popular
    /document-alias/live

    I have managed to get it to work with the following Rule

    RewriteRule ^(.*)/(.*)$ index.php?q=$1&template=$2 [L,QSA]

    However this breaks all existing urls without a final backslash /
    so /document-alias/rss
    or
    /document-alias/
    works
    but
    /document-alias
    does not.

    Can anyone see whats going wrong?
    A live example of where this rule could be used, is on the package repositry - a url like :
    http://modxcms.com/extras/repository/packages.html?tag=Forms

    could be rewritten like :
    http://modxcms.com/extras/repository/packages/tag/forms


    PS. Here are my current url settings in modx if it helps

    Use friendly URLs: Yes
    Prefix for friendly URLs:
    Suffix for friendly URLs:
    Use friendly aliases: Yes
    Use friendly alias path: No
    Allow duplicate aliases: Yes
    Automatically generate alias: Yes
      • 22303 MODX Staff
      • 10,725 Posts
      I would suggest separating it into two different RewriteRules, one whose rule matches the extra query string parameter and rewrites those, and one that does the normal MODx rewrite.
        • 19787
        • 28 Posts
        I spent a whole weekend learning some mod_rewrite voodoo, and thought I would share the results incase anyone else might be looking for RewriteRule examples.

        Please remember that this was for my particular setup described earlier and may not work for everyone.

        # This is used only for the root url calls  eg /rss or /popular or /live  - 
        # note. my root page has an alias of home (q=home)
        RewriteRule ^(rss|popular|live)/?$  index.php?q=home&template=$1 [L,QSA]
        
        
        # The Friendly URLs part
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        
        
        # This works for everything but root urls - the other Rewrite rule above fixes this
        RewriteRule ^/?([^/]*)/?([^/]*)/?$  index.php?q=$1&template=$2 [QSA]
        


        I have just learnt how to use the "request" and "tagging" extenders for Ditto, so I might try and do the same nice url scheme for those query strings.
          • 39036
          • 28 Posts
          Hey, I've been looking for a solution for converting extra query strings to friendly ones and this is perfect, Thanks mjm