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
. And if there is any wrong code here, please tell me about it.
Cheers