Foren / Cotonti / Support / I need help with mod_rewrite

mod_rewrite for dummies

GHengeveld
#28008 19. Januar 2011, 05:52
Aliases are needed for the URL rewrite to work. See the following rewriterule:
RewriteRule ^(.*?).html$ page.php?id=$1
In this rule, the value for (.*?) on the left is placed in place of $1 on the right (much like variables in PHP). The problem with this is that you can only use numbers, since you're giving the ID of the page. Therefore this rule will only work correctly for URLs like 1.html, 2.html etc. If you want to have URLs that look nice, by using the page title in the URL, you'll have to use the alternative method that Cotonti provides for calling a page, namely by using the 'al' query parameter instead of 'id'. This results in the following rule:
RewriteRule ^(.*?).html$ page.php?al=$1
This will allow you to call pages by their alias instead of their ID. In your case you don't have aliases, which makes that this doesn't work. Thats why you need aliases. Fortunately the autoalias2 plugin can automatically generate those, even for already existing pages. I suggest you follow Trustmaster's guidelines to make them look nicer. So, first you update the plugin with the new replacement code, then you run the UPDATE query to remove all existing aliases. Finally you run the plugin again so aliases are re-created for all of your pages.

The rewriting system in Cotonti consists of two parts: the Rewrite Rules in .htaccess, and the URL Transformation Rules in datas/urltrans.dat.

The Rewrite Rules allow your pages to be accessed through better looking URLs. This is not really related to Cotonti since it's just normal Apache server behavior (provided by the mod_rewrite Apache module).

The URL Transformation Rules are used by Cotonti's cot_url() function to make it output URLs that match the Rewrite Rules. This is needed because the Rewrite Rules only add aliases for URLs. So long as you don't update your site to actually use these aliases, nobody will know they exist (including Google). Cotonti uses the cot_url() function throughout. Basically this function gets a raw (old) URL, but split into separate parts for filename, query parameters (?a=b&c=d) and hash tag (#abc). It then loops through all the rules in urltrans.dat and looks for a match between those parts and any of the URL Transformation Rules. If it finds one, it will map the URL with the rule, basically doing the reverse of what a Rewrite Rule does. In the end all URLs in Cotonti will have a different format, determined by the transformation rules. Google will index these URLs. Of course, every URL Transformation Rule should have a matching Rewrite Rule. You see they work in pairs, one being the inverse of the other.

By the way, I recommend editing your datas/urltrans.dat directly using a text editor. It's less confusing than the admin tool and makes it easier to edit it in parallel to the .htaccess file.

Added 2 hours 2 minutes later:

Ok, here's a start.

urltrans.dat:
page	c=*	{list_url_structure()}
page	*	{page_url_structure()}
message	msg=*&redirect=*	message/{$msg}/{$redirect}
message	msg=*	message/{$msg}
admin	m=*	admin/{$m}
admin	*	admin
users	m=details&id=*&u=*	user/{$u}{!$id}
users	m=details&u=*	user/{$u}
users	m=edit&id=*	user/edit/{$id}
users	m=details	profile
users	m=profile	profile/edit
users	g=*&f=all&s=*&w=*	users/maingroup:{$g}/sort:{$s}-{$w}
users	g=*&f=*&s=*&w=*	users/maingroup:{$g}/filter:{$f}/sort:{$s}-{$w}
users	gm=*&f=all&s=*&w=*	users/group:{$gm}/sort:{$s}-{$w}
users	gm=*&f=*&s=*&w=*	users/group:{$gm}/filter:{$f}/sort:{$s}-{$w}
users	f=all&s=*&w=*	users/sort:{$s}-{$w}
users	f=*&s=*&w=*	users/filter:{$f}/sort:{$s}-{$w}
users	f=*	users/filter:{$f}
users	gm=*	users/group:{$gm}
users	g=*	users/maingroup:{$g}
users	m=register&a=add	register/add
users	m=register	register
users	m=logout&x=*	logout/{$x}
users	m=auth&a=check	login/check
users	m=auth	login
users	*	users
pm	*	pm
rss	*	rss
*	*	{$_area}.php

.htaccess:
RewriteRule ^admin$	                             admin.php [NC,NE,QSA,L]
RewriteRule ^admin/([a-z]+)$	                 admin.php?m=$1 [NC,NE,QSA,L]
RewriteRule ^login$                              users.php?m=auth [NC,NE,QSA,L]
RewriteRule ^logout/(.*)$                        users.php?m=logout&x=$1 [NC,NE,QSA,L]
RewriteRule ^users$                              users.php [NC,NE,QSA,L]
RewriteRule ^users/group:([0-9]+)$               users.php?gm=$1 [NC,NE,QSA,L]
RewriteRule ^users/maingroup:([0-9]+)$           users.php?g=$1 [NC,NE,QSA,L]
RewriteRule ^users/group:([0-9]+)/sort:([a-z]+)-(asc|desc)$   users.php?gm=$1&s=$2&w=$3 [NC,NE,QSA,L]
RewriteRule ^users/maingroup:([0-9]+)/sort:([a-z]+)-(asc|desc)$   users.php?g=$1&s=$2&w=$3 [NC,NE,QSA,L]
RewriteRule ^users/group:([0-9]+)/filter:(.*)$   users.php?gm=$1&f=$2 [NC,NE,QSA,L]
RewriteRule ^users/maingroup:([0-9]+)/filter:(.*)$   users.php?g=$1&f=$2 [NC,NE,QSA,L]
RewriteRule ^users/group:([0-9]+)/filter:(.*)/sort:([a-z]+)-(asc|desc)$   users.php?gm=$1&f=$2&s=$3&w=$4 [NC,NE,QSA,L]
RewriteRule ^users/maingroup:([0-9]+)/filter:(.*)/sort:([a-z]+)-(asc|desc)$   users.php?g=$1&f=$2&s=$3&w=$4 [NC,NE,QSA,L]
RewriteRule ^users/sort:([a-z]+)-(asc|desc)$     users.php?s=$1&w=$2 [NC,NE,QSA,L]
RewriteRule ^users/filter:(.*)$                  users.php?f=$1 [NC,NE,QSA,L]
RewriteRule ^users/filter:(.*)/sort:([a-z]+)-(asc|desc)$   users.php?s=$1&w=$2 [NC,NE,QSA,L]
RewriteRule ^user/(.*)$                          users.php?m=details&u=$1 [NC,NE,QSA,L]
RewriteRule ^user/edit/([0-9]+)$                 users.php?m=edit&id=$1 [NC,NE,QSA,L]
RewriteRule ^profile$                            users.php?m=details [NC,NE,QSA,L]
RewriteRule ^profile/edit$                       users.php?m=profile [NC,NE,QSA,L]

RewriteRule ^([0-9]+)$				 page.php?id=$1 [NC,NE,QSA,L]
RewriteRule ^([a-z0-9-/]+)/([0-9]+)$             page.php?id=$2 [NC,NE,QSA,L]
RewriteRule ^([a-z0-9-]+)$                       page.php?al=$1 [NC,NE,QSA,L]
RewriteRule ^([a-z0-9-/]+)/([a-z0-9-]+)$         page.php?al=$2 [NC,NE,QSA,L]
RewriteRule ^([a-z0-9-]+)/$                      page.php?c=$1 [NC,NE,QSA,L]
RewriteRule ^([a-z0-9-/]+)/([a-z0-9-]+)/$        page.php?c=$2 [NC,NE,QSA,L]

Custom functions:
function list_url_structure(&$args, &$spec)
{
    global $cot_cat;
    $url = str_replace('.', '/', $cot_cat[$args['c']]['path']).'/';
    unset($args['c']);
    return $url;
}
function page_url_structure(&$args, &$spec)
{
    global $cot_cat, $pag, $row, $rpagecat;
 
    $page_cat = (!empty($cot_cat[$rpagecat]['path']) && empty($page_cat)) ? $cot_cat[$rpagecat]['path'] : $page_cat;
    $page_cat = (!empty($cot_cat[$pag['page_cat']]['path']) && empty($page_cat)) ? $cot_cat[$pag['page_cat']]['path'] : $page_cat;
    $page_cat = (!empty($cot_cat[$row['page_cat']]['path']) && empty($page_cat)) ? $cot_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;
}

Dieser Beitrag wurde von Koradhil (am 19. Januar 2011, 07:56, vor 14 Jahre) bearbeitet