Форумы / Cotonti / Extensions / [RELEASE] Cotonti Awesome mod_rewrite URL System

1234>>>

Lombi
#1 29.03.2009 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>
Отредактировано: Kilandor (10.07.2009 03:08, 14 лет назад)
musiconly
#2 29.03.2009 19:37
And finally I have a strong reason to upgrade to cotonti :-)
thanks lombi
pieter
#3 29.03.2009 19:59
I tried it, but the site doesn't load anymore.

www.broens.be

Site loads if I disable .htaccess

Error at that moment:
Warning: Missing argument 2 for cot_main_urls(), called in /home/k3098kru/public_html/broens/system/functions.php on line 4492 and defined in /home/k3098kru/public_html/broens/system/functions.custom.php on line 124

---------------
Can you make a copy from clipbord for the .htaccess file?

Because I see a lot of tabs in it
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 ^(.*)/$ 	


Maybe I did a wrong copy paste.

http://www.broens.be/forums.php loads, but with errors
... can we help you ...
Отредактировано: pieter (29.03.2009 20:26, 15 лет назад)
fedai
#4 29.03.2009 20:59
Fatal error: Call to undefined function cot_main_urls() in /home/tsnet/public_html/system/functions.php on line 4359
"peace in the homeland, peace in the world" K.ATATÜRK

personal blog:
http://www.sametbal.net
Lombi
#5 29.03.2009 21:30
# fedai : Fatal error: Call to undefined function cot_main_urls() in /home/tsnet/public_html/system/functions.php on line 4359

You didnt do all the steps. This error clearly says you skipped this part:

undefined:
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');

# pieter : I tried it, but the site doesn't load anymore.

www.broens.be

Site loads if I disable .htaccess

Do you have msn?

Try this - http://www.vejica.com/functions.php.rar

Oh Oh! HTACCESS ISNT MADE FOR .BE DOMAINS :-))

replace all (com|org|si|net) with (com|org|si|net|be)
<a href="http://www.domenlo.com">Surreal Art</a>
Отредактировано: Lombi (29.03.2009 21:43, 15 лет назад)
Sean
#6 29.03.2009 23:00
Awesome work, Thanks for this Lombi! Any chance we could get this working for plugins? That's one other thing I would like to see.
Lombi
#7 29.03.2009 23:04
Which ones? I have this workig for a couple of my own plugins, but for plugins that havent been adapted to the new sed_url function you'd have to manually fix all of them.
<a href="http://www.domenlo.com">Surreal Art</a>
Brock
#8 29.03.2009 23:14
I love you #1.
Web Design Database - www.wddb.com
HarryRag
#9 29.03.2009 23:24
Should this also be able to test on a subdomain?
I'd rather test it there first on my contonti test domain, but also see a lot of www's where that wouldn't be in the url.

Should i replace in that case all the mentioned www's to cotonti (is name of testdomain)?
[center][url=<a href="">http://www.true-gamers.nl]True</a> &amp; Honest Gamers[/url][/center]
Lombi
#10 29.03.2009 23:29
Actually you cant test it on a subdomain due to the HTACCESS being universal for all three. In short this means that if you run it on a subdomain HTACCESS will thing it's a category.

You need to test it on a real domain or remove all the subdomain code (which is most of it)
<a href="http://www.domenlo.com">Surreal Art</a>
HarryRag
#11 29.03.2009 23:36
Ok, that saves me a lot of testing ;-)
Will be going to test later tonight then when the site is on low visitor hourz.

Just one other question, i was using the "save admin" for admin.php to restrict the acces to it and "gensitemap" (one of Tefra's plugins), can i still use this structure in .htacces?

RewriteEngine On
RewriteRule ^((urllist|sitemap_).*\.(xml|txt)(\.gz)?)$ plugins/gensitemap/datas/index.php?s=$1 [L]

<Files .htpasswd>
  deny from all
</Files>

<Files admin.php>
   <Limit GET POST PUT>
      require valid-user
   </Limit>
   AuthName "Access is Restricted"
   AuthType Basic
   AuthUserFile /locationtositeonserver_madeitlikeshitfornowasexample/anotherfolder/.htpasswd
</Files>
[center][url=<a href="">http://www.true-gamers.nl]True</a> &amp; Honest Gamers[/url][/center]
pieter
#12 29.03.2009 23:40
# Lombi :
Oh Oh! HTACCESS ISNT MADE FOR .BE DOMAINS :-))
replace all (com|org|si|net) with (com|org|si|net|be)

Harry you need to put in nl I guess
... can we help you ...
HarryRag
#13 30.03.2009 00:33
Was aware of that, thnx anyway for the reminder.
[center][url=<a href="">http://www.true-gamers.nl]True</a> &amp; Honest Gamers[/url][/center]
Sean
#14 30.03.2009 01:04
# Lombi : Which ones? I have this workig for a couple of my own plugins, but for plugins that havent been adapted to the new sed_url function you'd have to manually fix all of them.
Humm... alright. Well im working on a new portfolio and ive got two plugins id like to simplify the URLs for. Say I wanted www.mydomain.com/portfolio to go to plug.php?e=portfolio. How would I accomplish that? Bear with me, Im still trying to figure out this whole URL Rewrite thing.
HarryRag
#15 30.03.2009 08:01
Just have been testing on our live site.
Can't get it to work.

Followed the Start Post exactly.
Edited the files so a .nl domain would be accepted by adding the |nl to all those lines.

As soon as i had the files uploaded and hitted the F5 button to refresh, my running tractert at that time totally got timed out over all the hubs, from there it takes ages to get back to the site, as soon as i see the site back in the tractert and hit home/forums or any other link the time-outs are back and site not reachable with comment on it if my internet connection is ok/lost, but all other net brouwsing works like it should.

Also tried the supplied functions.php which says to be 003 on top in the file, but dbversion and version in this file are 002.
Line 42 and 43
$cfg['version'] = '0.0.2';
$cfg['dbversion'] = '0.0.2';
With that file on my 003 install all seems to be willing to load, but a snail where you just stepped on would be faster on it's trip around the globe then the site loads with it ;-) , half broken with only white background and stuff, no images loaded.

For me that raises the question, is this url rewrite configured/tested/created on 002 or 003 (or manually update) files or is it even tested on 004 beta, cause that would be another option to try.
Could XccS entries in the config.php to load that plug properly cause any problems?
[center][url=<a href="">http://www.true-gamers.nl]True</a> &amp; Honest Gamers[/url][/center]

1234>>>