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

    I have a custom-built holiday search which lists holiday offers.

    See below.

    // example output
    Holiday offer #1 (£1,000.00)
    Holiday offer #2 (£745.00)
    


    When a user clicks on a holiday offer they get taken to a page where I call a snippet.

    A user visits "http://modx.localhost.co.uk/offer/?offer_id=199"
    will call the offer page which calls a snippet to grab the offer id and 
    do some processing to get the right offer from the DB.
    


    What I want is to change it from /?offer_id=199 to something more search engine friendly such as:

    /offer/199/

    In the future I might want to put an actual custom built page alias phrase such as:

    /offer/NewYork-01Jan2009-QXBIKKK-199/

    Where I explode all - and grab the last one, assuming its numeric.

    What I want to know is the following.

    Q1) How do I change the RewriteRules to handle a single holiday offer (ie: /offer/199)?
    Q2) How do I change the RewriteRules to handle a custom built page alias (ie: /offer/NewYork-01Jan2009-QXBIKKK-199/)
    Q3) How do I ensure that the RewriteRules do not screw up any of the other pages?

    # The Friendly URLs part
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    


    I’m just not sure what I should be doing. Thanks.
      • 32645
      • 377 Posts
      I’ve tried this, but it doesn’t work;

      # The Friendly URLs part
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
      
      RewriteRule ^offer/([0-9][0-9])/$ /index.php?q=$1&offer_id=$2 [L,QSA]
      


      I want it to work even if offer is in a subdirectory such as /holidays/offer/1234/

      Thanks
        • 21257 MODX Staff
        • 730 Posts
        Try adding your rule *above* the standard modx rewrite directive. mod_rewrite isn’t even getting to your rule, because the one above it catches everything.

        RewriteRule ^offer/([0-9][0-9])/$ /index.php?q=$1&offer_id=$2 [L,QSA]
        
        # The Friendly URLs part
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
        


        Not that it will affect whether the rule works or not, but you may not need the "QSA" on the end. That tells mod_rewrite to append any query strings from the browser request onto your rewritten request. Depending on your app, you may or may not need that. Worst case, it would just use up some processing cycles that it doesn’t need to. (And using up unnecessary processor power is, of course, the very epitome of unsustainability tongue)
          Mike Schell
          Lead Developer, MODX Cloud
          Email: [email protected]
          GitHub: https://github.com/netProphET/
          Twitter: @mkschell
          • 21257 MODX Staff
          • 730 Posts
          .. and you may need to escape the slashes in the first URL

          RewriteRule ^offer\/([0-9][0-9])\/$ /index.php?q=$1&offer_id=$2 [L,QSA]
          
            Mike Schell
            Lead Developer, MODX Cloud
            Email: [email protected]
            GitHub: https://github.com/netProphET/
            Twitter: @mkschell
            • 32645
            • 377 Posts
            Thanks for your help, I really appreciate it!
              • 21257 MODX Staff
              • 730 Posts
              Sorry I didn’t notice this before...

              RewriteRule ^offer\/([0-9][0-9])\/$ /index.php?q=$1&offer_id=$2 [L,QSA]
              


              I see a couple of problems:
              * the q= needs to be the alias of the document that’s going to handle this request.
              * since you only have one set of parentheses in the first part, you don’t have a $2.. just a $1.
              * your rewrite rule will only act on URLs that contain two digits (the [0-9][0-9] part)
              * after consulting mod_rewrite docs again, none of their examples escape the forward slash, so I’m going to revert that suggestion (I recall having to do that one one or more servers though)

              So try

              RewriteRule ^offer/([0-9]+)/$ /index.php?q=offer&offer_id=$1 [L,QSA]
              


              You might have to play around with that alias in the redirect query string.. it might need to be "offer/" or something so that the modx parser can look up the proper document id.

              So that’s your Q1.

              Q2) How do I change the RewriteRules to handle a custom built page alias (ie: /offer/NewYork-01Jan2009-QXBIKKK-199/)
              If you know there will always be a dash before the offer id, then maybe this would work:
              RewriteRule ^offer/[^/]*\-([0-9]+)/$ /index.php?q=offer&offer_id=$1 [L,QSA]
              


              Q3) How do I ensure that the RewriteRules do not screw up any of the other pages?
              That rule will only apply to pages that match the pattern that we’re looking for. If you happened to want to create a modx document with an alias that does match this rule, without it being processed by the rule (quite unlikely I imagine), then some more thinking would be required.
                Mike Schell
                Lead Developer, MODX Cloud
                Email: [email protected]
                GitHub: https://github.com/netProphET/
                Twitter: @mkschell
                • 32645
                • 377 Posts
                Thanks again.
                  • 32645
                  • 377 Posts
                  Unfortunately my situation has changed and my client wants to use a structure such as the following.  There is no longer a unique id number.


                  /offer/MBJ7042C-montego_bay-LGW-sunset_beach_resort_spa/

                  How can I grab the part after the offer?  I need the whole string.
                    • 21257 MODX Staff
                    • 730 Posts
                    I’m not sure how the rules you quoted above correspond to your specific current problem. (They don’t map the incoming URL request to the MODx front controller.)

                    Anyway, the first part of the rule (known as the pattern part), to grab the string after the offer, assuming as in your example of /offer/MBJ7042C-montego_bay-LGW-sunset_beach_resort_spa/ that the offer and the string will always be separated by a dash and that the URL will always end in a trailing slash:
                    ^offer/[a-zA-Z0-9]+\-(.*)/$
                    


                    Here’s the official word on rule syntax (for Apache 1.3 anyway):
                    http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule
                      Mike Schell
                      Lead Developer, MODX Cloud
                      Email: [email protected]
                      GitHub: https://github.com/netProphET/
                      Twitter: @mkschell
                      • 32645
                      • 377 Posts

                      I’m not sure how the rules you quoted above correspond to your specific current problem. (They don’t map the incoming URL request to the MODx front controller.)

                      I did that by mistake, I apologise.

                      I tried your solution, and it doesn’t work. Perhaps I’m doing something wrong. Also, if I try to visit any other modx page, I get 404 or 500 errors.

                      # The Friendly URLs part
                      RewriteCond %{REQUEST_FILENAME} !-f
                      RewriteCond %{REQUEST_FILENAME} !-d
                      RewriteRule ^offer/[a-zA-Z0-9]+\-(.*)/$ index.php?q=offer&offer_id=$1 [L,QSA]
                      #
                      # This rule works \/
                      #RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
                      


                      Just to confirm, I just need to grab anything after the /offer/.