Forums / Cotonti / Extensions / [RELEASE] Cotonti Awesome mod_rewrite URL System

Lombi
#10670 2009-03-29 19:16
Hey. As I've already started discussing in this topic I'm releasing the code we're using on cotonti installations to get awesome URLs and more awesome rankings in the search engines.

Guide for rewriting http://www.cotonti.com/docs/en/admin/url_modification

This system is currently live on over 300 cotonti powered sites.

Before we start, some stuff you should know:
- it doesnt rewrite everything since it wasnt built for that, it was built for my own needs. It rewrites categories (first page), pages and forums. It's also doing users but i took that code out cause of compatibility with some of the sites i have this running on.
- it's customizable - as you'll see in the config.php bit you can set it to anything and the changes will be seen live
- pages are alias ONLY - the algorythm I use does not factor in pages. I'm using my AutoALIAS code to "force" all users to put in aliases (it autofills alias from title)

Step 1:

Put the following setting into config.php

$cfg['urls_pattern'] = '2'; //1=old, 2=cat/page.html, 3=subdomains, 4=cat/page/

What you get is the following below:

url type 2:

site.com/cateogry/
site.com/category/alias.html
site.com/forum/topic-t1.html

Live demos:
http://www.arcusband.com/
http://www.anavrin.net/

url type 3:
cateogry.site.com
cateogry.site.com/alias.html
forum.site.com/topic-t1.html

Live demos:
http://www.okusno.com/
http://www.lombergar.com/

url type 4:
site.com/cateogry/
site.com/category/alias/
site.com/forum/topic-t1.html

Live demos:
http://www.recepti.in/

Once you set it in config.php put the following line into system/functions.php right below define('SED_GROUP_TOPADMINS', 5);:

include('functions.custom.php');

So here we go, put this into functions.custom.php (the file does not exist, make one and upload it to your system directory)

<?PHP
if (!defined('SED_CODE')) { die('Wrong URL.'); }

function makesafe($text)
{
	//$text = preg_replace("#&\#([0-9]*);#ie","_",$text);
	$text = html_entity_decode($text);
	$text = str_replace(" / ","_",$text);
	$text = str_replace("'","",$text);
	$text = str_replace("\"","",$text);
	$text = str_replace("/","_",$text);
	$text = str_replace("'","_",$text);
	$text = str_replace(" - ","_",$text);
	$text = str_replace("-","_",$text);
	$text = str_replace(" ","_",$text);
	$text = str_replace( "ä", "ae", $text);
	$text = str_replace( "ö", "oe", $text);
	$text = str_replace( "ü", "ue", $text);
	$text = str_replace( "Ä", "Ae", $text);
	$text = str_replace( "Ö", "Oe", $text);
	$text = str_replace( "Ü", "Ue", $text);
	$text = str_replace( "ß", "ss", $text);
	$text = str_replace( "&", "and", $text);
	$text = str_replace( "%", "Percent", $text);
	$text = str_replace( "ó", "o", $text);
	$text = ereg_replace("[^A-Za-z0-9_-]", "", $text);
	$text = str_replace("____","_",$text);
	$text = str_replace("___","_",$text);
	$text = str_replace("__","_",$text);
	$text = strtolower($text);
	return $text;
}

function cot_category_url(&$args)
{
	global $cfg;
	$url = parse_url($cfg['mainurl']);
	$www = strstr($url['host'], 'www.') ? 'www' : '';
	$domain = str_replace('www.', '', $url['host']);
	$path = $url['path'];

	$patterns = array(
	'list.php?c={$c}',
	'/{$c}/',
	'http://{$c}.{$domain}/',
	'/{$c}/'
	);
	$url = str_replace(array('{$c}', '{$domain}'), array($args['c'], $domain), $patterns[$cfg['urls_pattern'] - 1]);
	unset($args['c']);
	return $url;
}

function cot_page_url(&$args)
{
	global $cfg, $pag, $row, $rpagecat;
	$url = parse_url($cfg['mainurl']);
	$www = strstr($url['host'], 'www.') ? 'www' : '';
	$domain = str_replace('www.', '', $url['host']);
	$path = $url['path'];

	$patterns = array(
	'/page.php?al={$alias}',
	'/{$c}/{$alias}.html',
	'http://{$c}.{$domain}/{$alias}.html',
	'/{$c}/{$alias}/'
	);
	$page_cat = (!empty($rpagecat) && empty($page_cat)) ? $rpagecat : $page_cat;
	$page_cat = (!empty($pag['page_cat']) && empty($page_cat)) ? $pag['page_cat'] : $page_cat;
	$page_cat = (!empty($row['page_cat']) && empty($page_cat)) ? $row['page_cat'] : $page_cat;
	$url = str_replace(array('{$c}', '{$alias}', '{$domain}'), array($page_cat, $args['al'], $domain), $patterns[$cfg['urls_pattern'] - 1]);
	unset($args['al']);
	return $url;
}

function cot_forum_section_url(&$args)
{
	global $cfg, $fsn, $row, $row1, $master, $title;
	$stitle = $title;
	$url = parse_url($cfg['mainurl']);
	$www = strstr($url['host'], 'www.') ? 'www' : '';
	$domain = str_replace('www.', '', $url['host']);
	$path = $url['path'];

	$title = (!empty($fsn['fs_title'])) ? $fsn['fs_title'] : $row['fs_title'];
	$title = (empty($title)) ? $stitle : $title;
	$title = (empty($title)) ? $master[1] : $title;
	$title = (empty($title)) ? $row1['fs_title']  : $title;

	$patterns = array(
	'/forums.php?m=topics&q={$id}',
	'/forum/{$title}-s{$id}.html',
	'http://forum.{$domain}/{$title}-s{$id}.html',
	'/forum/{$title}-s{$id}.html'
	);
	$url = str_replace(array('{$id}', '{$title}', '{$domain}'), array($args['s'], makesafe($title), $domain), $patterns[$cfg['urls_pattern'] - 1]);
	unset($args['s'], $master);
	return $url;
}

function cot_forum_topic_url(&$args)
{
	global $cfg, $fsnn, $fsn, $row, $ft_title;
	$url = parse_url($cfg['mainurl']);
	$www = strstr($url['host'], 'www.') ? 'www' : '';
	$domain = str_replace('www.', '', $url['host']);
	$path = $url['path'];
	
	
	$title = (!empty($fsnn['fs_lt_id'])) ? $fsnn['fs_lt_id'] : $fsn['fs_lt_id'];
	$title = (empty($title)) ? $row['ft_title'] : $title;
	$title = (empty($title)) ? $ft_title : $title;
	
	$patterns = array(
	'/forums.php?m=posts&q={$id}',
	'/forum/{$title}-t{$id}.html',
	'http://forum.{$domain}/{$title}-t{$id}.html',
	'/forum/{$title}-t{$id}.html'
	);
	$url = str_replace(array('{$id}', '{$title}', '{$domain}'), array($args['q'], makesafe($title), $domain), $patterns[$cfg['urls_pattern'] - 1]);
	unset($args['q']);
	return $url;
}

function cot_main_urls(&$args, &$spec)
{
	global $cfg;
	$url = parse_url($cfg['mainurl']);
	$www = strstr($url['host'], 'www.') ? 'www' : '';
	$domain = str_replace('www.', '', $url['host']);
	$path = $url['path'];
	$patterns = array(
	'/forums.php',
	'/forum/',
	'http://forum.{$domain}/',
	'/forum/'
	);
	if(empty($args) && $spec['_area'] == 'forums')
	{
		$url = str_replace(array('{$domain}'), array($domain), $patterns[$cfg['urls_pattern'] - 1]);
	}
	else
	{
		$url = $spec['_area'].'.php';
	}
	return $url;
}

?>

Almost there... :-D

Now we need to edit your urltrans.dat file inside of the datas/ folder... Put this in:

plug	e=tags&a=&t=*	{$e}/{$a)/{$t}.html
forums	m=posts&q=*	{cot_forum_topic_url()}
forums	m=topics&s=*	{cot_forum_section_url()}
page	al=*	{cot_page_url()}
list	c=*	{cot_category_url()}
*	*	{cot_main_urls()}

And now for the last step! Let's edit our .htaccess file which should be in the root of your Cotonti installation:

Options +FollowSymlinks
RewriteEngine On
#RewriteBase /

# AddType application/x-httpd-php .php .html
#AddHandler php-cgi .php .html
#Action php-cgi /cgi-bin/php.cgi


# **** NEW FORUMS ****
RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^forum\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^$ 					forums.php [L]

RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^forum\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^(.*)-t([0-9]*)\.html(.*)$ 			forums.php?m=posts&q=$2$3 [L]

RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^forum\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^(.*)-f([0-9]*)\.html(.*)$ 			forums.php?m=topics&s=$2$3 [L]

RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^forum\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^(.*)-s([0-9]*)\.html$ 			forums.php?m=topics&s=$2$3 [L]

# **** NEW PAGE/LIST REWRITE ****
RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} !^(forum|forums|gallery|main)\.([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^(.+)\.html(.*)$ /page.php?al=$1&comments=1$2 [L]

RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} !^(forum|forums|gallery|main)\.([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^$ list.php?c=%1 [L]

RewriteCond %{HTTP_HOST} ^main\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^$ 				http://www.%1.%2/ [R=301,L]

RewriteCond %{HTTP_HOST} ^main\.([^.]+)\.(com|org|si|net)/?$
RewriteRule ^(.+)$ 				http://www.%1.%2/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !^/(.*)/$
RewriteCond %{REQUEST_URI} !^/(.*)/(.*)\.html$
RewriteCond %{REQUEST_URI} !^/(links|blogs|bloglist)\.html$
RewriteCond %{REQUEST_URI} !^/blog-(.*)\.html$
RewriteCond %{REQUEST_URI} !^/(.*)-b([0-9]*)\.html$
RewriteRule ^(.*)\.html(.*)$ 				page.php?al=$1&comments=1$2 [L]

RewriteRule ^forum/$ 									forums.php [L]
RewriteRule ^forum/(.*)-t([0-9]*)\.html(.*)$ 			forums.php?m=posts&q=$2$3 [L]
RewriteRule ^forum/(.*)-f([0-9]*)\.html(.*)$ 			forums.php?m=topics&s=$2$3 [L]
RewriteRule ^forum/(.*)-s([0-9]*)\.html$ 				forums.php?m=topics&s=$2$3 [L]
RewriteRule ^tags/(.*)\.html$ plug.php?e=pagesearch&what=$1 [L]
RewriteRule ^(.*)/(.*)\.html(.*)$						page.php?al=$2&comments=1$3 [L]
RewriteRule ^(.*)/$ 									list.php?c=$1 [L]

## *** Catch all for any /*/ ****
#RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.(com|org|si|net)?$
#RewriteCond %{REQUEST_URI} !^/(users|forum|forums|reviews|main|tags)/(.*)\.html$
#RewriteRule ^(.*)/(.*)\.html(.*)$ 				http://$1.%2.%3/$2.html$3 [R=301,L]

#RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.(com|org|si|net)?$
#RewriteCond %{REQUEST_URI} !^/(users|forum|forums|reviews|main|tags)/$
#RewriteRule ^(.*)/$ 						http://$1.%2.%3/ [R=301,L]

# **** SITEMAP ****
RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^sitemap\.xml$ /rss.php?m=sitemap&cat=%1 [L]

# **** OTHER ****
RewriteRule ^users/(.*)\.html(.*)$ 						users.php?m=details&name=$1 [L]

# **** SAFETY ****

RewriteCond %{QUERY_STRING} (\"|%22).*(\>|%3E|<|%3C).* [NC]
RewriteRule ^(.*)$ redirect.html [NC]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC]
RewriteRule ^(.*)$ redirect.html [NC]
RewriteCond %{QUERY_STRING} (\;|\'|\"|\%22).*(union|insert|where|select|update|drop|md5|or|and|if).* [NC]
RewriteRule ^(.*)$ redirect.html [NC]
RewriteRule (,|;|<|>|'|`) redirect.html [NC]

# **** MESSAGES ****   
RewriteCond %{HTTP_HOST} !^(www\.)?([^.]+)\.(com|org|si|net)?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.(com|org|si|net)?$
RewriteRule ^message\.php(.*)$ 				http://www.%2.%3/message.php$1 [R,L]
RewriteRule ^(.*)/message\.php(.*)$ /message.php$3 [R,L]   
RewriteRule ^(.*)/(.*)/message\.php(.*)$ /message.php$4 [R,L]   

# **** ERROR MESSAGES **** 

ErrorDocument 400 /plug.php?e=error&m=400
ErrorDocument 401 /plug.php?e=error&m=401
ErrorDocument 403 /plug.php?e=error&m=403
ErrorDocument 404 /plug.php?e=error&m=404
ErrorDocument 500 /plug.php?e=error&m=500

RewriteCond %{HTTP_HOST} ^([^.]+)\.(com|org|si|net) [NC] 
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]

RewriteRule ^index.php / [R=301,L]

And we're done :)
<a href="http://www.domenlo.com">Surreal Art</a>
This post was edited by Kilandor (2009-07-10 03:08, 14 years ago)