Forums / Cotonti / Support / I need help with mod_rewrite

12>>>

mod_rewrite for dummies

aiwass
#1 2011-01-18 22:37
I've been searching in the forums and taken a look at the documentation, but still I don't know what to do. It's hieroglyphs to me, this whole thing with mod_rewrite. All I want is nice URLS on my website instead of the page.php?id=5517 etc. Can anyone help on how to do this and also setting it up, basically a "mod_rewrite for dummies" for Cotonti in mind?!

My site structure (category names)
news
- sitenews
- housepacific
- writeup
- bio
- lyrics
- interviews
- top100
- mpndj
- wtop
- comp
- newsflash
- megamind
- trackoftheday
- spotify
- dj-charts
- mixes
- debate
- global-news
- releases

festivalguides
- festivals2009
-- nordic09
-- eu09
- festivals2008
-- nordic08
-- eu08
- festivals2007
-- nordic07
-- eu07
- festivals2006
-- nordic06
-- eu06
- festivals2005
-- nordic05
-- eu05

Anyone want to help? I badly need it done and I would be thankful for all and any help making it a reality.
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
#2 2011-01-19 01:40
I can help you, but first you should decide on what you want the URLs to look like in the end. This is crucial for SEO, and changing it afterwards is not really an options (SEO-wise but also because of dead links). Things to think of:

- Do you want to use 'subfolders', so the path to a file in the CMS is also the path in the URL? This complicates things a lot but looks much better.
- Do you have other site areas to rewrite, such as forums?
- File extension (.html seems to be best for SEO, but no extension at all looks better)

I think it's a good idea to develop a SEO-friendly and cross-site implementation here, so we can put it in documentation, or even provide it with Cotonti by default.

Here's an example:
/path/to/somefolder => list.php?c=somefolder
/somefolder => list.php?c=somefolder
/path/to/somepage.html => page.php?al=somepage
/somepage.html => page.php?al=somepage
/path/to/someID.html => page.php?id=someID
/someID.html => page.php?id=someID
/forums/somesection/sometopictitle.html => forums.php?m=posts&q=sometopicID
/path/to/somefolder.rss => rss.php?c=somefolder
This post was edited by Koradhil (2011-01-19 01:50, 13 years ago)
Kingsley
#3 2011-01-19 03:09
Ok, confused noob here :)

isn't this kinda what the page aliases plug-in does?
GHengeveld
#4 2011-01-19 03:16
If you mean the autoalias plugin, that's something entirely different.
Try a google search for mod_rewrite and you'll know what we mean.
aiwass
#5 2011-01-19 03:20
My idea (if feasible) is to the following structure/paths:
www.megamind.se/debate/
www.megamind.se/labelnews/
www.megamind.se/djcharts/
according to the list in my initial post above etc. etc.

And pages within those above stated categories (this might be the hardest one):
www.megamind.se/debate/a-vote-is-everything-in-a-poll/ => www.megamind.se/page.php?id=4861
www.megamind.se/labelnews/shifted-presents-drained/ => www.megamind.se/page.php?id=5509
www.megamind.se/djcharts/alexi-delano-january-chart/ => www.megamind.se/page.php?id=5485
according to the list in my initial post above etc. etc.

OR

www.megamind.se/debate/4861/ => www.megamind.se/page.php?id=4861
www.megamind.se/labelnews/5509/ => www.megamind.se/page.php?id=5509
www.megamind.se/djcharts/5485/ => www.megamind.se/page.php?id=5485
according to the list in my initial post above etc. etc.

FORUMS
I'm going to delete the forums from the site, since no one is using them, they are only available for members anyway.

Is it doable?
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
#6 2011-01-19 03:25
Yes, can be done, but it will require you to set a page alias for every page. The above mentioned autoalias2 plugin can help with that. Also it will require a custom function that generates the URLs based on category structure.
aiwass
#7 2011-01-19 03:30
So basically I have to set aliases for roughly 6000 pages in order to get mod_rewrite happening on my website?

I should have gone for Wordpress back in 2002, then this problem wouldn't have existed. FUCK!


So, 5155 pages now have aliases, but it ain't pretty!
Ex: http://www.megamind.se/page.php?al=track-of-the-day---18th-january-2011
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-19 03:42, 13 years ago)
Trustmaster
#8 2011-01-19 04:30
A patch for alias generating function, open plugins/autoalias2/inc/autoalias2.functions.php at line 31 and replace
$title = str_replace(' ', $cfg['plugin']['autoalias2']['sep'], $title);
with
$title = preg_replace('#\s+#', $cfg['plugin']['autoalias2']['sep'], $title);
and then try to reassign aliases to those pages (using Admin => Tools => AutoAlias2). Before that you might need to reset your aliases with an SQL query:
UPDATE sed_pages SET page_alias = '';
or edit problematic aliases manually.
May the Source be with you!
aiwass
#9 2011-01-19 04:38
How does aliases then work with a mod_rewrite?
The URLS now are longer then before with just ID. I really need a URL rewrite in order to get this working better, cause Aliases are not really what I'm looking for, as stated above.
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
Trustmaster
#10 2011-01-19 05:25
It is a second step. Now you can edit your URLs (Admin => Other => URLs) so that you add such rules to your datas/urltrans.dat:
list	c=*	/{$c}/
page	*	{page_url_structure()}
*	*	{$_area}.php
Page URLs would need a function, since they don't have category names in their parameters. Create a file system/functions.custom.php and add a function to make page urls:
function page_url_structure(&$args)
{
	global $sed_cat, $pag, $row, $rpagecat, $c, $newpagecat;


	$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;
	//$page_cat = (!empty($sed_cat[$args['c']]['path']) && empty($page_cat)) ? $sed_cat[$args['c']]['path'] : $page_cat;
	$page_cat = (!empty($sed_cat[$newpagecat]['path']) && empty($page_cat)) ? $sed_cat[$newpagecat]['path'] : $page_cat;

	if (empty($page_cat)) $page_cat = 'articles';
	if($args['id'])
	{
		$url = $page_cat.'/';
		$url .= $args['id'] . '/';
		unset($args['id']);
	}
	elseif($args['al'])
	{
		$url = $page_cat.'/';
		$url .= urlencode($args['al']) . '/';
		unset($args['al']);
	}
	return $url;
}

And finally add mod_rewrite rules to your .htaccess:
# Rewrite engine options
Options FollowSymLinks -Indexes
RewriteEngine On
# Path to Cotonti
RewriteBase /

# Protect static paths
RewriteRule (datas|images|js|skins|plugins|system)/(.*)$ $1/$2 [L]

# Pages by ID
RewriteRule ([a-z]+)/([0-9]+)/ page.php?id=$2 [QSA,NC,NE,L]
# Pages by alias
RewriteRule ([a-z]+)/([a-zA-Z0-9\-_]+)/ page.php?al=$2 [QSA,NC,NE,L]
# Category
RewriteRule ([a-z]+)/ list.php?c=$1 [QSA,NC,NE,L]
May the Source be with you!
GHengeveld
#11 2011-01-19 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;
}
This post was edited by Koradhil (2011-01-19 07:56, 13 years ago)
aiwass
#12 2011-01-19 17:25
@Koradhil
What I've noticed is that error messages are URL rewritten into http://www.megamind.se/message/950 but nothing else is working as it should. I'm reverting back to alias so that my visitors can enjoy the site while the problem is solved. I've tried both Trustmasters way and your way, but it just won't work. Annoying and rather strange, since I believe you guys know what you are doing, I don't :)

Now my site just displays the header, but without any CSS cosmetics and the source looks like this and it's HTML5:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Megamind Magazine</title>
<meta charset="UTF-8">
<meta name="description" content="Megamind Magazine - A online magazine focusing on DJ Culture and Clubbing in and around the Nordic part of Europe." />
<meta name="keywords" content="Megamind, Megamind Magazine" />
<meta name="robots" content="index,follow">
<meta name="Language" content="en">
<meta name="netinsert" content="0.0.1.4.2.9.1">
<meta name="blogcatalog" content="9BC8730366">
<meta name="alexaVerifyID" content="YMfQhwBI40KNTYIzIWPsepWPZAE">
<link rel="stylesheet" href="http://www.megamind.se/skins/2009/2009.css">
<link rel="alternate" type="application/rss+xml" title="RSS" href="rss.php?c=news">
<link rel="alternate" type="application/rss+xml" title="Sitemap File XML" href="sitemap.xml">
<link rel="canonical" href="http://www.megamind.se">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="js/base.js"></script>
<base href="http://www.megamind.se/" />
<!-- TradeDoubler site verification 3084 -->
</head>
<body id="top">
<div id="network">
<div class="center-wrapper">
<header>
<div class="left"><a href="index.php" title="Megamind Magazine">Megamind Magazine</a> <span class="text-separator">|</span> <span class="quiet">Life is a Vacation</span></div>
<div class="right">

<ul class="tabbed" id="network-tabs">
<li><a href="http://twitter.com/#search?q=%23megamind" title="#megamind on Twitter">#megamind</a></li>
<li><a href="http://twitter.com/megaminds/megartists" title="@megaminds/megartists on Twitter">@megaminds/megartists</a></li>
<li><a href="http://www.facebook.com/megamindmagazine" title="Megamind Magazine Fan Page on Facebook">Facebook Fan Page</a></li>
</ul>
<div class="clearer">&nbsp;</div>
</div>
</header>
<div class="clearer">&nbsp;</div>
</div>
</div>
<div id="site">
<div class="center-wrapper">
<div id="header">

<div class="right" id="toolbar"><a href="plug.php?e=twitti&amp;a=signin"><img src="plugins/twitti/img/signin.png" alt="Sign in with Twitter" width="150" height="22"></a></div>
<div class="clearer">&nbsp;</div>
<div id="site-title"><a href="index.php" title="Home"><img src="cdn-icons/megamind-magazine-new-logo.png" width="322" height="81" alt="This is the new Megamind Magazine logo"></a></div>
<nav>
<div id="navboard">
<div class="nbcolumn left" id="nbcolumn-1">
<div class="nbcolumn-content">
<div class="nbcolumn-title"><a href="page.php?id=2939" title="The Articles">The Articles</a></div>
<ul class="plain-list">
<li><a href="list.php?c=debate" title="Debate">Debate</a></li>
<li><a href="list.php?c=newsflash" title="Newsflash">Newsflash</a></li>
<li><a href="list.php?c=globalnews" title="International News">International News</a></li>
<li><a href="list.php?c=labelnews" title="Label News">Label News</a></li>

<li><a href="list.php?c=reviews" title="Chronicles &amp; Reviews">Chronicles &amp; Reviews</a></li>
<li><a href="list.php?c=interviews" title="Interviews">Interviews</a></li>
</ul>
</div>
</div>
<div class="nbcolumn left" id="nbcolumn-2">
<div class="nbcolumn-content">
<div class="nbcolumn-title"><a href="page.php?id=2938" title="The Playlists">The Playlists</a></div>
<ul class="plain-list">
<li><a href="list.php?c=djcharts" title="DJ Charts">DJ Charts</a></li>
<li><a href="list.php?c=spotify" title="Spotify Playlists">Spotify Playlists</a></li>

<li><a href="list.php?c=housepacific" title="Housepacific Playlists">Housepacific Playlists</a></li>
<li><a href="list.php?c=mixes" title="Mixes & Podcasts">Mixes & Podcasts</a></li>
<li><a href="list.php?c=trackoftheday" title="Track of the day">Track of the day</a></li>
</ul>
</div>
</div>
<div class="nbcolumn left" id="nbcolumn-3">
<div class="nbcolumn-content">
<div class="nbcolumn-title"><a href="page.php?id=2936" title="The Industry">The Industry</a></div>
<ul class="plain-list">
<li><a href="list.php?c=biographies" title="Artist Biographies">Artist Biographies</a></li>

<li><a href="list.php?c=lyrics" title="EDM Lyrics">EDM Lyrics</a></li>
<li><a href="list.php?c=competition" title="Competitions">Competitions</a></li>
<li><a href="list.php?c=mpndj" title="Most Popular Nordic DJ">Most Popular Nordic DJ</a></li>
<li><a href="list.php?c=top100" title="DJ Mag Top 100 DJs">DJ Mag Top 100 DJs</a></li>
</ul>
</div>
</div>
<div class="nbcolumn left normargin" id="nbcolumn-4">
<div class="nbcolumn-content">
<div class="nbcolumn-title"><a href="page.php?id=2937" title="The Site">The Site</a></div>
<ul class="plain-list">
<li><a href="list.php?c=sitenews" title="Site News">Site News</a></li>

<li><a href="page.php?id=1" title="About Megamind Magazine">About Megamind Magazine</a></li>
<li><a href="plug.php?e=contactus" title="Contact Us">Contact Us</a></li>
<li><a href="page.php?id=1707" title="Shop">Shop</a></li>
<li><a href="plug.php?e=search&amp;tab=pag" title="Regular Search">Regular Search</a></li>
<li><a href="plug.php?e=tags&amp;a=pages" title="Tag Cloud Search">Tag Cloud Search</a></li>
</ul>
</div>
</div>
<div class="nbcolumn right nolmargin" id="nbcolumn-5">
<div class="nbcolumn-content">
<div class="nbcolumn-title"><a href="page.php?id=1330" title="The Members">The Members</a></div><ul class="plain-list">

<li><a href="users.php?m=auth" title="Login">Login</a></li>
<li><a href="users.php?m=register" title="Become a member">Become a member</a></li>
<li><a href="plug.php?e=passrecover" title="Lost your password?">Lost your password?</a></li>
</ul></div>
</div>
<div class="clearer">&nbsp;</div>
</div>
</nav>
</div>
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
#13 2011-01-19 17:55
1. Check if $cfg['customfuncs'] is set to TRUE in datas/config.php.
2. In my above example, replace page.php?c= with list.php?c= (it's a Siena thing), for both .htaccess and urltrans.dat.
3. Try emptying your site cache (admin -> other -> internal cache).
aiwass
#14 2011-01-19 18:07
Take a look at the site now and you'll see that it ain't working. I've done all the steps you've mentioned.

The only parts that work with the URL rewrite is megamind.se/admin/ and megamind.se/profile/ and nothing else, like pages and lists.

In the functions.custom.php there is reference to cot_cat but that should be sed_cat.
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-19 18:16, 13 years ago)
GHengeveld
#15 2011-01-19 18:31
Correct, in Genoa it's sed_cat.
Remember that you have to modify the Menu entries in the adminpanel to contain your new URLs.

12>>>