We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Quote from: ReSpawN at Aug 03, 2010, 07:11 AM

    Yes, but that redirect would take care of the rewritten url, say: /page-2/, which will eventually redirect to ?page=2. That’s simply not what I want, but it is possible non the less. The reason why I used the .htaccess rules is to create FURLs which will stay in place all the time, everytime.
    I think you are misunderstanding. No redirect necessary; you can route them as is using $modx->sendForward(). This works to allow friendly URLs without any .htaccess requirements.
      • 24865
      • 289 Posts
      I haven’t even thought about that! laugh Bare in mind, this is my second week into MODx ánd programming. No prior experience before 26 july hehe!

      I’m gonna give that a try and when I work that out successfully, perhaps this could be a nice addition to this already awesome snippet.
        @MarkGHErnst

        Developer at Adwise Internetmarketing, the Netherlands.
        • 1434
        • 23 Posts
        Great idea, is fantastic the powerfull and posibilities of modx.

        In my case, with differents urls using .html suffix I have used a different approach and also to avoid dupplicate pages between index and page 1 (I have modify the page var to thepage to avoid conflicts whit another apps):

        <?php
        if ($modx->event->name === 'OnWebPagePrerender') {
            $output = $modx->resource->_output;
            $output = preg_replace('%.html\?thepage=(?!1)(\d+)%mis', '/pagina-$1.html', $output);
            $output = preg_replace('%.html\?thepage=1%mis', '.html', $output);
            $modx->resource->_output = $output;
        }
        ?>
        


        Example urls:

        http://domain/url/file.html?pagina=1 --> http://domain/url/file.html
        http://domain/url/file.html?pagina=2 --> http://domain/url/file/pagina-2.html
        
          • 12491
          • 90 Posts

          I think you are misunderstanding. No redirect necessary; you can route them as is using $modx->sendForward(). This works to allow friendly URLs without any .htaccess requirements.

          Could you give a example of a function like that,
          I tried to folow the Archivist furls
          But it isn’t a good example for me becouse of it’s complicity.


          thnx
          Hurby
            Sommige mensen hebben aan een half woord genoeg
            • 28988
            • 1 Posts
            Quote from: ReSpawN at Aug 02, 2010, 09:08 AM

            The MODx Plugin, inits on the OnWebPagePrerender event
            <?php
            	if ($modx->event->name === 'OnWebPagePrerender') {
            		$output = $modx->resource->_output;
            		$output = preg_replace('%/(.+?)/\?page=(.+?)%mis', '$1/pagina-$2/', $output);
            	
            		$modx->resource->_output = $output;
            	}
            ?>


            Hi,

            after spending some time trying to make this work for me, I’ve noticed the problem: missing "/" in code right before "$1" in preg_replace. So this line:
            $output = preg_replace('%/(.+?)/\?page=(.+?)%mis', '$1/pagina-$2/', $output);

            needs to be:
            $output = preg_replace('%/(.+?)/\?page=(.+?)%mis', '/$1/pagina-$2/', $output);


            This missing "/" made me a lot of troubles, because the final source code on a web page was changed.

            For example
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

            changed to
            <!DOCTYPE html PUBLIC "-W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


            and some of </a> became <a> ...

            Also, An-tonio made a good point in avoiding duplicate pages between index and page 1. So i added 1 more line in code, to change the link to page 1 into the index page.

            $output = preg_replace('%/(.+?)/pagina-1/%mis','/$1/', $output);


            And for all of you who are not sure (just like me when I first found this topis) what exactly to do with this code, here is a brief instructions:

            First of all, create new plugin, and name it whatever you like.

            Paste this code in that plugin:
            <?php
            if ($modx->event->name === 'OnWebPagePrerender') {
            $output = $modx->resource->_output;
            $output = preg_replace('%/(.+?)/\?page=(.+?)%mis', '/$1/pagina-$2/', $output);
            $output = preg_replace('%/(.+?)/pagina-1/%mis','/$1/', $output);
            $modx->resource->_output = $output;
            }
            ?>
            

            Move to TAB named "System Events", scroll to bottom, and check the box next to: "OnWebPagePrerender".
            Click save.

            After that, open your htaccess file and find this code:
            # The Friendly URLs part
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
            


            Add 2 lines of code
            # The Friendly URLs part
            RewriteCond %{REQUEST_URI} /(.+?)/pagina-(.+?)/ [NC]
            RewriteRule (.*) /%1/?page=%2 [L,QSA,NC]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
            

            And that should do the trick.

            You can also change "pagina" to any word you like (translation of "page" to your language).

            So the URLs will look like this:
            http://www.somedomain.com/dir/?page=3   ->   http://www.somedomain.com/dir/pagina-3/
            and
            http://www.somedomain.com/dir/?page=1  ->  http://www.somedomain.com/dir/
            


            All of this works for me just great.

            Since i am newbie here, I would really like to hear if this work for you also smiley. And if there is any wrong code here, please tell me about it.

            Cheers
              • 24865
              • 289 Posts
              I already came across the same solution about 2 days ago, should’ve posted it. smiley
                @MarkGHErnst

                Developer at Adwise Internetmarketing, the Netherlands.
                • 69
                • 22 Posts
                What if you don’t want /pagina-2 /pagina-3 etc.. but just /2 and /3 etc?

                If I remove the pagina- in the .htaccess MODx stops working

                So if I change:
                RewriteCond %{REQUEST_URI} /(.+?)/pagina-(.+?)/ [NC]

                Into:
                RewriteCond %{REQUEST_URI} /(.+?)/(.+?)/ [NC]

                All hell breaks loose tongue.. any other solution to just get /number ?
                • I had the same question but another thought crossed my mind:

                  Static URLs should represent a unique resource. With getPage, the content is ment to change at least periodically. Wouldnt it be wiser to tweak the rewriting to work with i.e. with the tvFilter? That would kind of guarantee the same content for a "full" page (which contains $limit resources).

                  Dont ask me how, it just crossed my mind smiley