Jump to content

[SOLVED] help with replace dynamic url into static url


Recommended Posts

Hi

 

I have a code that will convert my Dynamic urls to static.. I've already used mod_rewrite which is ok but now have to use php to convert them according to the rewrited one.

 

However with php, instead of having this:

 

http://www.mu-anime.com/page/OngoingAnime/

 

I'm having this:

 

http://www.mu-anime.com/index/page/OngoingAnime/

 

The 'index' is to much there and i dont have a clue how to remove it. Below are the relevant codes

 

 

init.php

define('BASE_URL', 'http://www.mu-anime.com/'); // this you must use as an absolute path to the application root, ex: /folder1/folder2/

if (isset($_SERVER['PATH_INFO'])) {
$url = substr($_SERVER['PATH_INFO'], 1);
$urlParts = explode('/', $url);
if ($urlParts[count($urlParts) - 1] == '') array_pop($urlParts);

$urlPartsCount = count($urlParts);
if ($urlPartsCount % 2 != 0) {
	$urlPartsCount++;
}
for ($i = 0; $i < $urlPartsCount; $i += 2) {
	$_GET[$urlParts[$i]] = $urlParts[$i + 1];
}
} 

$urlPatterns = array(
'~'.preg_quote(BASE_URL).'([^\.]+[0-9a-zA-Z]+).php(\?([0-9a-zA-Z]+[^#"\']*))?~i',
); 


ob_start();

 

deinit.php

function urlRewriteCallback($match) {
$extra = '';
if ($match[3]) {
	$params = explode('&', $match[3]);
	if ($params[0] == '') array_shift($params);
	foreach ($params as $param) {
		$paramEx = explode('=', $param);
		$extra .= $paramEx[0].'/'.$paramEx[1].'/';
	}
}
return BASE_URL.$match[1].'/'.$extra;
} 


$pageContents = ob_get_contents();
ob_end_clean();
echo preg_replace_callback($urlPatterns, 'urlRewriteCallback', $pageContents); 

 

Someone help me with this please :(

$string is a generic variable, used to show the 3rd argument in str_replace.  $subject is also a common one.  In other words, you need to put your own variable in there.  Whatever variable that has your url you want changed.

i've been able to remove /page/ by changing this: 

 

$extra .= $paramEx[0].'/'.$paramEx[1].'/';

 

to this:

 

$extra .= $paramEx[1].'/';

 

but still, the index is here.. dont have a clue how to remove it.. i should use an if statement maybe but dont know how

i noticed something..

 

if my link is like this -> index.php?action=register

 

when i change return BASE_URL.$match[1].'/'.$extra;

 

to return BASE_URL.$extra;

 

it worked welll....

 

but if my link is like this

 

http://www.mysite.com/page.php

 

it doesn't work.. i get only the domain name of my site without the page.php

Fixed

 

here's how i did it.. again it was a str_replace :D

 

on deinit.php:

 

<?php

function urlRewriteCallback($match) {
$extra = '';
if ($match[3]) {
	$params = explode('&', $match[3]);

	if ($params[0] == '') array_shift($params);
	foreach ($params as $param) {
		$paramEx = explode('=', $param);

		$extra .= $paramEx[1].'/';

	}
}

	return BASE_URL.$match[1].'/'.$extra;
} 



$pageContents = ob_get_contents();
ob_end_clean();

$pregit = preg_replace_callback($urlPatterns, 'urlRewriteCallback', $pageContents); 

$preg = str_replace('index/','',$pregit);

echo $preg;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.