Cotonti URL Modifcation Cotonti URL Modifcation

Detailed guide with examples on how to modify your URL's

#1. -=== Intro ===-

In Cotonti we decided to add a easy system for re-writing URLs without the need for core hacking. This allows admins to easily design URLs in any fashion they want. This guide was written for 0.0.6 and greater, there are some things that might not be in older versions that appear in this guide. The first 0.0.6 beta does not include a couple of the tweaks needed for this

#2. -=== Basic Setup ===-

  1. You need to create a .htaccess file if you don't already have one. This should be uploaded to your root web folder (the place with all your index/page/list/etc .php files. If you don't want to manually create rules this needs to be CHMOD 777. For this guide you will be manually creating your URLS. However the URL Rewerite does offer basic rules creation.
  2. You need to make sure that datas/urltrans.dat
  3. is CHMOD 777

#3. -=== Basic URLs Tool Details ===-

This portion will detail the basics of the URLs page, and of what each option does.
First off to access the URL modifications tool goto Admin > Other > URL or go straight to admin.php?m=urls
  • New Rule - This adds a new rule to the above list
  • Area - This is the area the rule is for, it corresponds to "*".php
  • Parameters - This is the URL you want to match for(modify)
  • Format - This is what the URL will be formated into
  • Delete - This well.... Deletes the Rule :)
  • Overwrite .htaccess? - This will when checked, save the basic generated rules straight to .htaccess Not recommended if you are manually creating them
  • Changing Order(Hidden) - To change the order of a rule simply click on a row(rule) and drag it into the position you want.Please note this requires javascript enabled
  • Save - Saves any changes made :)

#4. -=== Basic URLs Modifcation & Details ===-

Most of this information i'm about to provide is listed in the Help at the bottom of the URL page, but I will try to explain it in more detail.

The first thing to note is that don't include area.php or ? you just start with something=something

Parameters - This is the URL your trying to match
  • * - This may be used to match against any url for a area or may be used to match anything for a variable
    1. Example 1: * - This will cause it to be called for all URLs
    2. Example 2: id=* - This will cause it to match if id is in the url and is not restricted by any value it has.
  • | - This may be used to match against a specific value for a variable
    1. Example 1: id=1|2|3|15 - This will cause it to match any URL that contains ID but has the value of 1,2,3, or 15 no others will be matched
Format - This is what your creating the output of the URL to look like
  • {$_area} - This may be used to output the Area which the url is for
    1. Example 1: {$_area}.html
  • {$_host} - This may be used to output the Hostname (domain) from your $cfg['mainurl']
    1. Example 1: http://{$_host}/
  • {$_rhost} - This may be used to output the Hostname (domain) from the current HTTP request (the URL they accessed the site with)
    1. Example 1: http://{$_rhost}/
  • {$_path} - This may be used to output the server path to your site(if your site is in root its / or else its the path inside to the site)
    1. Example 1: {$_path}/something.html
  • {$variables} - This may output the contents of any variable into the url
    1. Example 1: {$al}.html
    2. Example 2: {$id}.html
  • {!$variables} - This can be used to unset variables that are in the url you do not want, if you do not do this they will be tagged onto the end with ? . The postion of this does not matter, but it would be recommended for visual sake to place it at the end
    1. Example 1: users/{$u}{!$m}{!$id}
  • {callback_function()} - This is used for advanced URL manipulation, which allows you to edit the URL via a function. You can combine it with the above, but it would be recommended to do everything you need or want in the function.
    1. Example 1: {my_url_callback()}

#5. -=== Basic URLs Tool Examples ===-

These are some basic examples you can use that will actually work!

Pages

Page by ID
  • Area: - page
  • Parameters: - id=*
  • Format: - {$_area}/{$id}
  • Output Example: - page/1
  • .htaccess Example: -
    RewriteRule ^page/([0-9]+)$ page.php?id=$1 [QSA,NC,NE,L]
Page by Alias
  • Area: - page
  • Parameters: - al=*
  • Format: - {$_area}/{$al}
  • Output Example: - page/myfancypage
  • .htaccess Example: -
    RewriteRule ^page/([a-zA-Z0-9_]+)$ page.php?al=$1 [QSA,NC,NE,L]
Users

Users (to go along with this set it should be below the other users rules)
  • Area: - users
  • Parameters: - *
  • Format: - {$_area}/
  • Output Example: - users/
  • .htaccess Example: -
    RewriteRule ^users/$ users.php [QSA,NC,NE,L]
Users * by id
  • Area: - users
  • Parameters: - m=*
  • Format: - {$_area}/{$m}/{$id}{!$u}
  • Output Example: - users/details/1
  • .htaccess Example: -
    RewriteRule ^users/([a-z]+)/$ users.php?m=$1 [QSA,NC,NE,L]
    RewriteRule ^users/([a-z]+)/([0-9]+)$ users.php?m=$1&id=$2 [QSA,NC,NE,L]
Users * by Username
  • Area: - users
  • Parameters: - m=*
  • Format: - {$_area}/{$m}/{$u}{!$id}
  • Output Example: - users/details/Kilandor
  • .htaccess Example: -
    RewriteRule ^users/([a-z]+)/$ users.php?m=$1 [QSA,NC,NE,L]
    RewriteRule ^users/([a-z]+)/([^?]+)$ users.php?m=$1&u=$2 [QSA,NC,NE,L]

#6. -=== Advanced URLs Tool Examples ===-

This will show you how to use callbacks and is considered advanced, due to you have to manually create any htaccess rules, as well as creating PHP functions.

First off if your going to use callbacks you will have to include them, this may be done very easily.
  • Create a file in system/ named functions.custom.php
  • The first code snippet in this file should be
    if (!defined('SED_CODE')) { die('Wrong URL.'); }
  • Open your datas/config.php find $cfg['customfuncs'] = FALSE; and set it to true
This will cause the functions.custom.php to be included in functions.php, without any need for modifications.

This will explain the basics variables / information
  • &$args - This contains all the variables of the URL
    1. Example 1: $args['id']
  • &$spec - This contains a few special things that was listed above in the basic section, _area, _host, _rhost, and _path
    1. Example 1: $spec['_area']
  • unset($var) - Similar to the way the basic variable works any args that are not unset will be tagged along to the end of the url
    1. Example 1: unset($args['id'])
  • return value - The returned value is what the url will be ouput as
    1. Example 1: return $url
List

Category directories
  • Area: - list
  • Parameters: - c=*
  • Format: - {list_url_structure()}
  • Function: -
    /**
     * Changes  to / for List URLS
     *
     * @param array $args Args passed over from sed_url
     * @param array $spec Special info passed over from sed_url
     * @return string
     */
    function list_url_structure(&$args, &$spec)
    {
    	global $sed_cat;
    	
    	$url = str_replace('.', '/', $sed_cat[$args['c']]['path']).'/';
    	unset($args['c']);
    	
    	return $url;
    }
  • Output Example: - articles/ or news/subcategory/
  • .htaccess Example: -
    RewriteRule ^(articles|news)/$ list.php?c=$1 [QSA,NC,NE,L]
    RewriteRule ^(articles|news)/([a-z-]+)/$ list.php?c=$2 [QSA,NC,NE,L]
    RewriteRule ^(articles|news)/([a-z-]+)/([a-z-]+)/$ list.php?c=$3 [QSA,NC,NE,L]

Pages

Pages alias and id with Category directories
  • Area: - page
  • Parameters: - *
  • Format: - {page_url_structure()}
  • Function: -
    /**
     * Changes  to / for Page URLS
     *
     * @param array $args Args passed over from sed_url
     * @param array $spec Special info passed over from sed_url
     * @return string
     */
    function page_url_structure(&$args, &$spec)
    {
    	global $sed_cat, $pag, $row, $rpagecat;
    
    	$page_cat = (!empty($sed_cat[$rpagecat]['path']) && empty($page_cat)) ? $sed_cat[$rpagecat]['path'] : $page_cat;
    	$page_cat = (!empty($sed_cat[$pag['page_cat']]['path']) && empty($page_cat)) ? $sed_cat[$pag['page_cat']]['path'] : $page_cat;
    	$page_cat = (!empty($sed_cat[$row['page_cat']]['path']) && empty($page_cat)) ? $sed_cat[$row['page_cat']]['path'] : $page_cat;
    	$url =  str_replace('.', '/', $page_cat).'/';	
    	if($args['id'])
    	{
    		$url .= $args['id'];
    		unset($args['id']);
    	}
    	else
    	{
    		$url .= urlencode($args['al']);
    		unset($args['al']);
    	}
    	return $url;
    }
  • Output Example: - category/id or category/subcategory/alias
  • .htaccess Example: -
    #for page id
    RewriteRule ^(articles|news)/([a-z-]+)/([a-z-]+)/([a0-9]+)$ page.php?id=$4 [QSA,NC,NE,L]
    RewriteRule ^(articles|news)/([a-z-]+)/([0-9]+)$ page.php?id=$3 [QSA,NC,NE,L]
    RewriteRule ^(articles|news)/([0-9]+)$ page.php?id=$2 [QSA,NC,NE,L]
    
    #for page alias
    RewriteRule ^(articles|news)/([a-z-]+)/([a-z-]+)/([a-zA-Z0-9-_]+)$ page.php?al=$4 [QSA,NC,NE,L]
    RewriteRule ^(articles|news)/([a-z-]+)/([a-zA-Z0-9-_]+)$ page.php?al=$3 [QSA,NC,NE,L]
    RewriteRule ^(articles|news)/([a-zA-Z0-9-_]+)$ page.php?al=$2 [QSA,NC,NE,L]

Please note the reason for the first set of names, is used to distinguish base directories to match, this can avoid complications with complex rewrites. Change the base to match your base categories or add more as needed. This only supports up 2 sub cats, but it can be copied to go deeper.

#7. -=== Basic htacces Information ===-

This section will give a few small tips for when building rules, it will not be a complete guide on how to build them

Flags - Flags are what is contained in [ ]
  • QSA - Query String Append(QSA) causes any "GET"(?) data left in the url to still be sent along and is allowed to be processed
  • NC - No Case(NC) causes the pattern to be considered Case-Insensitive
  • NE - No Escape(NE) prevents the normal URI encoding of special characters to hexcode equivalent
Patterns
  • escaping - As in PHP some special characters need to be escaped such as ".","(",")","[","]","\" if for some reason you have these in your pattern, you must place a "\" before them to escape them
    1. Example 1 - soemthing\.html


1. Trustmaster  10.07.2009 03:21
Great article, good job on the URLs!
2. Spuner  10.07.2009 04:51
Thanks.
But there are some problems. :(
"Users * by Username" isn't worked. It's give links "users/details" and redirect to users list
Also "Page by Alias" and "Page by ID" are conflicting. Parsing only that, what stay higher in list that is in admin.php?m=urls.
3. tensh  10.07.2009 15:04
Great article, I hope I'll manage to get it to work :)

... what about plugins? :)
4. tensh  10.07.2009 15:36
About page alias - seems not to work, maybe because I'm using special chars like _ in my page alias, how htaccess would look like if I wanted to use special characters?
Or maybe indeed there's id/alias conflict
5. tensh  10.07.2009 16:27
By the way, the "append rule" thing doesn't work, it always overwrites the first form.
I'm using Firefox.
6. tensh  10.07.2009 16:40
Sorry to spam the comments, but 10 minutes is not much time to edit :)
when using user by username style nice urls, the error appears:

Warning: urlencode() expects parameter 1 to be string, array given in /home/sth/ftp/cotonti/system/functions.php on line 4568

I'm using Cotonti beta 0.0.6
7. Kilandor  11.07.2009 15:09
Update:

Fixed the Page by Alias Rewrite rule
Added a rule to Username *

Tested them all all are working. Please note a couple things in this guide require 0.0.6 beta 2 or higher.
8. tensh  11.07.2009 19:43
Great, great, great article, thanks Kilandor! :-)
I managed to make most of it work.

I'll try to write a function for forums (to display converted forum topics in url), it would be a blast! And very google-friendly.
9. Spuner  11.07.2009 22:05
Thanks, it's all works. :-)
But one question. Plugin "news" have out links: site.com/(page id or alias), but need to: site.com/news/(page id or alias). I try to add $cat to $pag['page_pageurl'], but it's only for "more". PAGE_ROW_URL is generating by sed_url.
10. Kilandor  12.07.2009 12:37
@Spuner If you use the 2 callback functions, it will work how your wanting to have (like we have here)

Those 2 functions are almost the exact we have here, except I removed the special bit for documents sections
11. tensh  12.07.2009 18:11
I have a question: Does this url rewriting cooperate with pagi18n plugin by Trustmaster? (localizations)?
12. Trustmaster  14.07.2009 06:26
Yes, it does. See a live demo here
13. tensh  17.07.2009 17:37
What's the progress on page add redirection bug?
14. tensh  02.10.2009 18:45
I'd like to ask if the bug of url redirection after adding a page is solved, or maybe propose a function to solve it?
15. Nafanya  13.10.2009 15:49
Show you how to solve the problem with the addition of pages?

Всего: 17, на странице: 15 12>>>

Добавление комментариев доступно только зарегистрированным пользователям