We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6699
    • 11 Posts
    Hi Guys,

    I’ve spent some hours trying to find out how to emulate Apache’s Mod_Rewrite functionality on my ISP’s Zeus shared server.

    I found the following resources useful:
    The code written by romracer on the Drupal forum is shown below, and I have tried amateurishly to modify it to work with MODx when installed in a subdirectory (substitute [tt]subdir[/tt] for your installation path). Please excuse this as I have little or no coding experience and of course take no responsibility or credit:

    RULE_0_START:
    # get the document root
    map path into SCRATCH:DOCROOT from /
    # initialize our variables
    set SCRATCH:ORIG_URL = %{URL}
    set SCRATCH:REQUEST_URI = %{URL}
    
    # see if we're in the subdir
    match URL into $ with ^(\/subdir)(.*)$
    if matched then
      set SCRATCH:SUB_DIR = $1
      set SCRATCH:REQUEST_URI = $2
    endif
    
    # see if theres any queries in our URL
    match URL into $ with ^(\/subdir)(.*)\?(.*)$
    if matched then
      set SCRATCH:SUB_DIR = $1
      set SCRATCH:REQUEST_URI = $2
      set SCRATCH:QUERY_STRING = $3
    endif
    RULE_0_END:
    
    RULE_1_START:
    # prepare to search for file, rewrite if its not found
    set SCRATCH:REQUEST_FILENAME = %{SCRATCH:DOCROOT}
    set SCRATCH:REQUEST_FILENAME . %{SCRATCH:SUB_DIR}
    set SCRATCH:REQUEST_FILENAME . %{SCRATCH:REQUEST_URI}
    
    # check to see if the file requested is an actual file or
    # a directory with possibly an index.  don't rewrite if so
    look for file at %{SCRATCH:REQUEST_FILENAME}
    if not exists then
      look for dir at %{SCRATCH:REQUEST_FILENAME}
      if not exists then
        set URL = %{SCRATCH:SUB_DIR}/index.php?q=%{SCRATCH:REQUEST_URI}
        goto QSA_RULE_START
      endif
    endif
    
    # if we made it here then its a file or dir and no rewrite
    goto END
    RULE_1_END:
    
    QSA_RULE_START:
    # append the query string if there was one originally
    # the same as [QSA,L] for apache
    match SCRATCH:ORIG_URL into % with \?(.*)$
    if matched then
      set URL = %{URL}&%{SCRATCH:QUERY_STRING}
    endif
    goto END
    QSA_RULE_END:
    


    Hope I can save someone some time with this (or perhaps it could be included in the documentation somewhere)!

    Cheers,

    Dave
      • 28948
      • 42 Posts
      Dave, I can’t thank you enough. Brilliant! Worked out-of-the-box

      cal.
        --
        cal wilson
        oksushi.com
        • 25663 MODX Staff
        • 12,272 Posts
        Interesting and thanks for the information. What version of Zeus are you using out of curiosity? Also, why Zeus?
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 13089
          • 2 Posts
          Hello Thanks for your help.

          I have just taken over maintenance of a site for a friend and am used to Apache rewrite. I’m just a novice at this stuff so bear with me.

          I am using namesco server which uses Zeus and their support suggested "If the tool validated the code then save the code through a test editor and call the file ’rewrite.script’. You then need to upload the rewrite.script file to the web root folder of the hosting." I did this then when into tools/config in the admin of modx and turned on friendly seo’s. When I click on the link’s I get page not found. Any suggestions? Do the page’s need to all have aliases as the id no. just comes up. Here is the site link without the seo on. http://www.soberholidays.ie.

          Any help would be appreciated.
            • 13089
            • 2 Posts
            anybody?
              • 23247
              • 6 Posts
              Hi,

              This works great...except that it seems to also be rewriting my manager folder. Does anybody know how to include the exclusion of the manager and assets folders?

              Thanks!
                • 20371
                • 58 Posts
                Quote from: Dave at May 18, 2007, 12:08 AM


                Hope I can save someone some time with this (or perhaps it could be included in the documentation somewhere)!



                I’m stuck with a zeus webserver for the minute (soon migrating to apache) and was dreading having to mess around with the mod rewrite stuff (convert apache to zeus) because I know how much of a headache that can be.

                Your provided code worked ’out of the box’ - and you undoubtedly have saved me hours. Cant thank you enough - your a legend! smiley
                  • 20371
                  • 58 Posts
                  I’ve run into some problems using zeus rewrite.. specifically, 301 redirects dont work for addresses within the folder specified for modx.

                  is it possible to include a redirect in this script?

                  I only need to redirect one page.

                  Thanks..
                    • 11180
                    • 10 Posts
                    I am on namesco, I sent support a detailed message to say htaccess was not working, whilst I was waiting for a reply I found this and tried to implement it, I couldn’t get it working! However since namesco have replied to me regarding their server being on Zeus I have tried this again and it now works!

                    They must have changed something their end...

                    Does anyone know how I can remove the subdir in the URL string that is shown to my visitors?

                    From - www.mydomain.com/modx/page1.html to www.mydomain.com/page1.html




                      • 24168
                      • 45 Posts
                      Hi guys,

                      I have been playing around with Revolution on a Zeus host and have updated the rewrite script for you all to use:

                      ## REWRITE .DOMAIN.COM to WWW.DOMAIN.COM
                      match IN:Host into $ with ^DOMAIN\.com$          
                      if matched          
                        match URL into $ with ^/(.*)$          
                        if matched          
                          set OUT:Location  = http://www.DOMAIN\.com/$1          
                          set OUT:Content-Type = text/html          
                          set RESPONSE = 301          
                          set BODY = Moved
                          goto END       
                        endif          
                      endif
                      
                      # Ignore anything to do with manager
                      match URL into $ with (/manager/|/core/|/connectors/|/assets/)     
                      if matched
                           goto END
                      endif
                      
                      map path into SCRATCH:REQUEST_URI from %{URL}           
                      # Look for the file
                      look for file at %{SCRATCH:REQUEST_URI}           
                      if exists 
                          goto END
                      endif
                      # Look for dir
                      look for dir at %{SCRATCH:REQUEST_URI}           
                      if exists 
                          goto END
                      endif 
                      
                      match URL into $ with ^/(.*)$
                      if matched  
                          set URL = /index.php?q=$1
                          goto END
                      endif
                      


                      I hope this is of some help!
                        Stewart
                        Freelance Web Designer & Developer:
                        http://www.qodo.co.uk

                        My extras:
                        TwitterX http://modx.com/extras/package/twitterx
                        TableOfContentsX http://modx.com/extras/package/tableofcontentsx