Reply
Can someone help me with this mod_rewrite code?
Old 04-01-2007, 10:09 AM Can someone help me with this mod_rewrite code?
imported_Emil's Avatar
$100 - $999 Monthly

Posts: 173
I'm using Cute News on my site and I want to use a mod_rewrite addon for it.

But as the code is now, it will make the links look like this:

http://www.yoursite.com/news/post/th...ews-title.html

and the comments page will have a link like this:

http://www.yoursite.com/news/comment...ews-title.html

But I would like to have it look like this instead:

http://www.yoursite.com/news/this-is...news-title.php

So I want to remove the "post" and "comments" parts and I want it to be .php instead of .html.

And I want the link to stay they same no matter where on the site you are. I mean.. No matter if you click a headline on www.yoursite.com or on www.yoursite.com/subfolder/, it should still take you to http://www.yoursite.com/news/this-is...news-title.php

If someone could please take a look at the code below and let me know what I would need to change, that would be highly appreciated. I tried to change the parts that I thought was the obvious parts, but that didn't work.

Here's the code:

Code:
//Mod_rewrite start

function replace_title($title){
$letter_replacements = array('ö' => 'oe', 'ä' => 'ae');
	$result = strtolower(strip_tags(html_entity_decode($title)));
	$result = strtr($result, $letter_replacements);
	$result = preg_replace('/\W/', "-", strip_tags($result));
	
	return $result;
}

function title_to_id($title){
global $path_to_data;

	if($handle = opendir($path_to_data."/archives"))
		while (false !== ($file = readdir($handle))){
			if($file != "." and $file != ".." and eregi("news", $file)){
				$files_arch[] = $path_to_data."/archives/$file";
			}
		}
	$files_arch[] = $path_to_data."/news.txt";

foreach($files_arch as $file){
	$all_news = file($file);
	foreach($all_news as $news_line){
		$news_arr = explode("|", $news_line);
		if(replace_title($news_arr[2]) == $title){

			return $news_arr[0];
		}
	}
}
}

function title_to_ucat($title){
global $path_to_data;
	$all_news = file($path_to_data."/news.txt");
	foreach($all_news as $news_line){
		$news_arr = explode("|", $news_line);
		if(replace_title($news_arr[2]) == $title){
			return $news_arr[6];
		}
	}
}

function find_archive($id){
global $path_to_data;

	if($handle = opendir($path_to_data."/archives"))
		while (false !== ($file = readdir($handle))){
			if($file != "." and $file != ".." and eregi("news", $file)){
				$files_arch[] = $path_to_data."/archives/$file";
			}
		}
	$files_arch[] = $path_to_data."/news.txt";

	foreach($files_arch as $file){
		$all_news_db = file("$file");
		foreach($all_news_db as $news_line){
			$news_arr = explode("|", $news_line);
			if($news_arr[0] == $id){
				if(ereg("([[:digit:]]{0,})\.news\.arch", $file, $regs)){ $archive = $regs[1]; }
				return $archive;
			}
		}
	}
}

function id_to_title($id, $archiv = false, $type, $page = ''){
global $path_to_data;

	if($archiv){
	$all_news = file($path_to_data."/archives/".$archiv.".news.arch");
	}
	else{
	$all_news = file($path_to_data."/news.txt");
	}
		
	foreach($all_news as $news_line){
		$news_arr = explode("|", $news_line);
		if($news_arr[0] == $id){
			$title = replace_title($news_arr[2]);
			if($type == "commentspage"){
				return "commentspage/".$page."/".$title.".html";
			}
			else
			return $type."/".$title.".html";
		}
	}
}

$show_news = ob_get_contents();
ob_get_clean();

$script_file = end($script_url = explode("/", $HTTP_SERVER_VARS['SCRIPT_NAME']));
$path = substr($HTTP_SERVER_VARS['SCRIPT_NAME'], 0, -strlen($script_file));

$find = array(	/*post*/			"#".$script_file."\?subaction=showfull&id=(.*?)&archive=&start_from=&ucat=(.*?)&#ie",
				/*postcomments*/	"#".$script_file."\?subaction=showcomments&id=(.*?)&archive=&start_from=&ucat=(.*?)&#ie",
				/*postpage*/		"#".$script_file."\?start_from=(.*?)&ucat=&archive=&subaction=&id=(.*?)&#i",
				/*archiveindex*/	"#".$script_file."\?archive=(.*?)&subaction=list-archive#i",
				/*archive*/			"#".$script_file."\?subaction=showfull&id=(.*?)&archive=(.*?)&start_from=&ucat=(.*?)&#ie",
				/*archivepage*/		"#".$script_file."\?start_from=(.*?)&ucat=&archive=(.*?)&subaction=list-archive&id=&#i",
				/*archivecomments*/	"#".$script_file."\?subaction=showcomments&id=(.*?)&archive=(.*?)&start_from=&ucat=(.*)&#ie",
				/*searchnews*/		"#".$script_file."\?misc=search&subaction=showfull&id=(.*?)&archive=&cnshow=news&ucat=(.*?)&start_from=&#ie",
				/*searcharchives*/	"#".$script_file."\?misc=search&subaction=showfull&id=(.*?)&archive=(.*?)&cnshow=news&ucat=(.*?)&start_from=&#ie",
				/*ncommentspage*/	"#".$script_file."\?comm_start_from=(.*?)&archive=&subaction=showcomments&id=(.*?)&ucat=(.*?)&#ie",
				/*acommentspage*/	"#".$script_file."\?comm_start_from=(.*?)&archive=(.*?)&subaction=showcomments&id=(.*?)&ucat=(.*?)&#ie",
			);

$replace = array(	"id_to_title('\\1', false, 'post')",
					"id_to_title('\\1', false, 'comments')",
					"postpage/\\1.html",
					"archiveindex/\\1.html",
					"id_to_title('\\1', '\\2', 'archive')",
					"archivepage/\\2/\\1.html",
					"id_to_title('\\1', '\\2', 'comments')",
					"id_to_title('\\1', false, 'post')",
					"id_to_title('\\1', '\\2', 'archive')",
					"id_to_title('\\2', false, 'commentspage', '\\1')",
					"id_to_title('\\3', '\\2', 'commentspage', '\\1')",
				);

$show_news = preg_replace($find, $replace, $show_news);
$show_news = str_replace("&url=".$url, '', $show_news);

if(!file_exists('.htaccess')){
$htaccess = "RewriteEngine On\n";
$htaccess .= "RewriteBase ".$path."\n\n";
$htaccess .= "RewriteRule ^post/(.*).html(.*)$ ".$script_file."?subaction=showfull&url=$1$2 [L,NC]\n";
$htaccess .= "RewriteRule ^postpage/(.*).html(.*)$ ".$script_file."?start_from=$1$2 [L,NC]\n\n";
$htaccess .= "RewriteRule ^comments/(.*).html(.*)$ ".$script_file."?subaction=showcomments&url=$1$2 [L,NC]\n";
$htaccess .= "RewriteRule ^commentspage/(.*)/(.*).html(.*)$ ".$script_file."?comm_start_from=$1&subaction=showcomments&url=$2$3 [L,NC]\n\n";
$htaccess .= "RewriteRule ^archiveindex/(.*).html(.*)$ ".$script_file."?archive=$1&subaction=list-archive$2 [L,NC]\n";
$htaccess .= "RewriteRule ^archive/(.*).html(.*)$ ".$script_file."?subaction=showfull&url=$1$2 [L,NC]\n";
$htaccess .= "RewriteRule ^archivepage/(.*)/(.*).html(.*)$ ".$script_file."?start_from=$2&archive=$1&subaction=list-archive$3 [L,NC]";

$file = fopen(".htaccess", "w");
fwrite($file, $htaccess);
fclose($file);
}

echo $show_news;
//Mod_rewrite end
__________________
HipHopDefined.com
imported_Emil is offline
Reply With Quote
View Public Profile Visit imported_Emil's homepage!
 
When You Register, These Ads Go Away!
Reply     « Reply to Can someone help me with this mod_rewrite code?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12759 seconds with 12 queries