Forum Moderators: coopster

Message Too Old, No Replies

A "preg_replace" question

Regarding URL

         

henry0

1:24 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am still testing a form of rewrite via preg_replace and OB
I found a way to make it working and showing such an URL:
[localhost...]

Now I am trying to get rid of the URL section that reads:
"index.php?"

using:

<?
ob_start('post_process');
function post_process($buffer) {
return preg_replace("'index.php/?([^\"\']+)'e", "'index.php'.implode('-', preg_split('/&¦=/', '\\1')).'.html'", $buffer);
}
?>

But I cannot figure after many trials how to deal with the final part.

coopster

12:07 am on Mar 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Can you show a before and after example?

henry0

11:53 am on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This the previous one:
[localhost...]

and the new one:
[localhost...]

so the gain is only the addition of " .html "

and the .htaccess is:

RewriteEngine on
RewriteRule (index.php)-([^-]+)-([^-]+)\.html$ $1.html?$2=$3 [N,QSA]
RewriteRule index\.html index.php

Now I am totally baffled
I figured that I had to modif my Apache2 setup to turn mode_rewrite on
And now the results that I copied (above) are produced by the .htaccess REGARDLESS of the OB script
Why: I commented it out (The OB script) and get the same result with or without OB script
So now it is the reversed position the .htaccess works but the OB is useless (as is)
By the way I did borrow it from AndreaF in the bag of trick
I used OB because the added OB load on the server in my case is minimum
Thank you

coopster

12:31 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume you are referring to message #4 in the Bag-O-Tricks for PHP II [webmasterworld.com]?

So basically you have some existing pages which are already developed and you want to convert any URI prior to pushing it out to the browser? Then, in order for anybody to click and use that particular link you are using mod_rewrite.

I see two errors in your preg_replace function. The first question mark (?) needs to be escaped, you have your slash the wrong way. The second is that you are not stripping off the '.php' extension in your replacement value.

return preg_replace("'index.php\?([^\"\']+)'e", 
"'index-'.implode('-', preg_split('/&¦=/', '\\1')).'.html'",
$buffer);

Don't forget to replace the pipe symbol as the forum replaces it with a broken pipe symbol.

henry0

1:07 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes it comes from that ref page

Now I got a clean URL:
[localhost...]
but it results in a 404
is is due to the .htaccess?

Thank you

Henry

coopster

1:38 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You removed the .php, so you have to remove it in your RewriteRule as well.

RewriteEngine on
RewriteRule (index.*)-([^-]+)-([^-]+)\.html$ $1?$2=$3 [N,QSA]
RewriteRule index\.html index.php

henry0

1:53 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe some day I'll use my brain :)
thanks