Forums / Cotonti / Support / I need help with mod_rewrite

<<<12

mod_rewrite for dummies

aiwass
#16 2011-01-19 18:40
The thing is that everything except lists and pages work.

Here is an example:
I want to look in the list called "debate" and I type in www.megamind.se/news/debate and I'm transported to www.megamind.se/message/950 which is a blank page. If I instead write www.megamind.se/debate I go to the same place as before www.megamind.se/message/950. So my guess is that it has to do with these rules, with in the URLtrans.dat and .htaccess :

URLtrans.dat
list	c=*	{list_url_structure()}
page	*	{page_url_structure()}

.htaccess
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-]+)/$                      list.php?c=$1 [NC,NE,QSA,L]
RewriteRule ^([a-z0-9-/]+)/([a-z0-9-]+)/$        list.php?c=$2 [NC,NE,QSA,L]
Take all that money that we spend on weapons and defences each year and instead spend it feeding and clothing and educating the poor of the world, which it would many times over, not one human being excluded, and we could explore space, together, both inner and outer, forever, in peace. - Bill Hicks

https://evlear.com
GHengeveld
#17 2011-01-19 18:53
urltrans.dat is correct (take care of the tabs, they're important).

As for .htaccess, that looks good too. Just make sure you have this in there as well:

Options FollowSymLinks -Indexes -MultiViews
DirectoryIndex index.php

RewriteEngine On
RewriteBase /

RewriteRule ^(datas|images|js|lib|modules|plugins|themes)/(.*)$ $1/$2 [L]

Make sure that the rules for page/list are at the bottom in the .htaccess. Also, don't remove the other rules for admin,users etc, because otherwise those pages will become inaccessible. Add these rules for messages to work:

RewriteRule ^message/([0-9]+)$		message.php?msg=$1 [NC,NE,QSA,L]
RewriteRule ^message/([0-9]+)/(.*)$	message.php?msg=$1&redirect=$2 [NC,NE,QSA,L]

You can download my htaccess and urltrans here.
This post was edited by Koradhil (2011-01-19 18:59, 13 years ago)
aiwass
#18 2011-01-19 19:03
Well, it ain't working that's for sure. Everything except lists & pages. And to top things off http://www.megamind.se/index.php looks like it's missing a few things....

.htaccess
RewriteEngine On
RewriteBase /

RewriteRule (datas|images|js|skins|plugins|system)/(.*)$ $1/$2 [L]
RewriteCond %{REQUEST_URI} !=cdn-icons/megamind-icon.png
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 ^message/([0-9]+)$		message.php?msg=$1 [NC,NE,QSA,L]
RewriteRule ^message/([0-9]+)/(.*)$	message.php?msg=$1&redirect=$2 [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-]+)/$                      list.php?c=$1 [NC,NE,QSA,L]
RewriteRule ^([a-z0-9-/]+)/([a-z0-9-]+)/$        list.php?c=$2 [NC,NE,QSA,L]

urltrans.dat
list	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

functions.custom.php
defined('SED_CODE') or die('Wrong URL');
function list_url_structure(&$args, &$spec)
{
    global $sed_cat;
    $url = str_replace('.', '/', $sed_cat[$args['c']]['path']).'/';
    unset($args['c']);
    return $url;
}
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;
}

Added 3 days later:

Update!

The mod_rewrite now works, but with these rules and Rewrites, not the ones mentioned before in the thread:

urltrans
plug	e=contactus	contactus
rss	c=*	{$_area}/{$c}
list	c=*	{$_area}/{$c}
page	al=*	{$c}/{$al}
*	*	{$_area}.php

htaccess
RewriteEngine on
RewriteBase /
RewriteRule (datas|images|pics|cdn-icons|flyers|js|skins|plugins|system)/(.*)$ $1/$2 [L]
RewriteCond %{REQUEST_URI} !=cdn-icons/megamind-icon.png

RewriteRule ^rss/([^/&?#]+)(.*)$ rss.php?c=$1$2 [QSA,NC,NE,L]
RewriteRule ^contactus(.*)$ plug.php?e=contactus$1 [QSA,NC,NE,L]
RewriteRule ^list/([^/&?#]+)(.*)$ list.php?c=$1$2 [QSA,NC,NE,L]
RewriteRule ^([^/&?#]+)/([^/&?#]+)(.*)$ page.php?c=$1&al=$2$3 [QSA,NC,NE,L]
RewriteRule ^([^/&?#]+)/([^/&?#]+)(.*)$ page.php?c=$1&al=$2$3 [QSA,NC,NE,L]

functions.custom
<?php
defined('SED_CODE') or die('Wrong URL');

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;
}
function list_url_structure(&$args, &$spec)
{
	global $sed_cat;

	$url = str_replace('.', '/', $sed_cat[$args['c']]['path']).'/';
	unset($args['c']);

	return $url;
}

?>
Take all that money that we spend on weapons and defences each year and instead spend it feeding and clothing and educating the poor of the world, which it would many times over, not one human being excluded, and we could explore space, together, both inner and outer, forever, in peace. - Bill Hicks

https://evlear.com
This post was edited by aiwass (2011-01-22 21:25, 13 years ago)
aro747
#19 2014-01-03 07:55

I have completed this by createing the .htaccess and urltrans.dat file in the /datas directory.  Is that correct?

Does the URL Editor plugin need to be running for this to work and what setting for it?  

Right now its like nothing has happened.  All the old urls are working, nothing has changed.  

This is what I have in my site:

/index.php?e=page&c=biopharma&al=Gilead-Sciences

I would like to change it to this:

/profiles/biopharma/Gilead-Sciences

Thanks for your help.

This post was edited by aro747 (2014-01-03 08:06, 10 years ago)
Twiebie
#20 2014-01-03 10:42

The rules should be applied to your main .htaccess file, which should be in the root of your site.

Follow these instructions and you should be good to go:

http://www.cotonti.com/docs/admin/sef_urls#ch1

aro747
#21 2014-01-03 21:09

Thanks, that helped a lot.  Almost there.

Now I'm getting:

/profiles/biopharma/?al=Gilead-Sciences

I would like to make it:

/profiles/biopharma/Gilead-Sciences

 

Twiebie
#22 2014-01-03 21:23

Did you remove your own ./datas/urltrans.dat file that you created earlier? It's not needed if you are using the handy preset.

Don't forget to clear the cache in the administration panel after you made any changes.

aro747
#23 2014-01-03 21:29

Changed to 'Handy' from urltrans.dat and it works perfectly.  Thank you so much!  Really appreciate your help.

Twiebie
#24 2014-01-03 21:44

Awesome!

<<<12