Clean URL

Some people don’t like URLs like “www.mySite.com/?1st_level:2nd_level:3rd_level”. They rather prefer URLs like “www.mySite.com/1st_level/2nd_level/3rd_level.html“. Since CMSimple produces the first type of URL, we have to change something, that CMSimple produces the second type. And this is not so easy.

First of all, the method described here requires Unix/Linux servers. It wont work on windows servers. A next prerequisite is the mod_rewrite package which has to be loaded on your server and configured correctly by your ISP.

The method is a compilation and correction of a thread in CMSimple's Forum. It follows seven steps and has been tested on two different Linux servers with PHP5 one of which was a free FunPic server. The test environment was CMSimple v2.9 (this is important, since the line declarations in “Method” may vary in the different versions).

Method

1)
Edit the file cmsimple/cms.php. Add

if($cf['urlrewrite']['base']!='')$sn=$cf['urlrewrite']['base'];

on line 88 just above the line which says: // define su - selected url.

2)
Search for and replace function a() with

function a($i,$x) {
	global $sn,$u,$cf;
	return '<a href="'.$sn.($cf['urlrewrite']['base']?'':'?').$u[$i].$x.($cf['urlrewrite']['base']?'.html':'').'">';
}

You will find it around line 316 of cms.php.

3)
Edit the file cmsimple/config.php. Add at its very end the variable:

$cf['urlrewrite']['base']="/";

Add it just above the ?> sign. This works only if CMSimple is running in the root of your site. It CMSimple is running in a subdirectory you have to say

$cf['urlrewrite']['base']="/subdirectory/";

instead.


Now CMSimple produces URLs which look like www.mySite.com/1_level_page.html. It wont work, though, since the server still expects /?1_level_page. We have to tell him to understand both. To do this we activate mod_rewrite. And we do it with a .htaccess Linux configuration file.

4)
On your workstation generate a file called htaccess.txt. (Windows does not allow to generate a file called .htaccess.) fill this file with:

RewriteEngine on
# To prevent loop:
RewriteCond %{REQUEST_URI}  !index.php
# Do not rewrite requests to template or images dir:
RewriteCond %{REQUEST_URI}  !templates
RewriteCond %{REQUEST_URI}  !images
# Do not rewrite requests with any query strings:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.html$ /index.php?$1

Save it and load it on your server into the root or there where CMSimple‘s index.php file resides. Rename the file to .htaccess. (Do not forget the dot in front of name. It wont work without it.) In order to see this file now, your FTP client has to be configured to make hidden files visible.


Now it should already work! You will be able to work with URLs like www.mySite.com/1st_level.html. However second or third level pages will still turn up URLs like www.mySite.com/1st_level:2nd_level:3rd_level.html. If you can live with this you should stop here. However, if you edit cmsimple/config.php and change $cf[’uri’][’seperator’]=”:”; to $cf[’uri’][’seperator’]=”/”; (colon to slash), you will have slashes and no colons anymore. But your site is unable to find the stylesheet. The reason: a third level page has to look back for its stylesheet at ../../templates/your_template/stylesheet.css, where it usually does not look. It looks at ./templates/your_template/stylesheet.css. To correct this we have to change the head() function in cms.php. But please, be warned and test your website carefully, afterwards! It is possible that plugins or scripting may not work correctly anymore - especially if it is delt with paths. In this test, a switch_template_on_the_fly script was not performed correctly.


5)
Edit cmsimple/cms.php and add to line 80 the code:

$pth['uri']['stylesheet']=$cf['urlrewrite']['base'] . $pth['file']['stylesheet'];

It has to be placed below all the declaration of variables. Or it wont work.

6)
Search for and replace function head() with:

function head() {
	global $title,$cf,$pth,$tx,$hjs;
	if($cf['site']['title']!='')$t=$cf['site']['title'].' - '.$title;
	else $t=$title;
	$t='<title>'.strip_tags($t).'</title>';
	foreach($cf['meta'] as $i=>$k)$t.=meta($i);
	if($tx['meta']['codepage']!='')$t.=tag('meta http-equiv="content-type" content="text/html;charset='.$tx['meta']['codepage'].'"');
	return $t.tag('meta name="generator" content="CMSimple 2.9 - www.cmsimple.dk"').tag('link rel="stylesheet" href="'.$pth['uri']['stylesheet'].'" type="text/css"').$hjs;
}

You will find the function around line 390.

7)
Edit cmsimple/config.php and exchange

$cf['uri']['seperator']=":";

with

 $cf['uri']['seperator']="/";

Colon to slash.

And you are done.


Important Note at the End

This will not work, if you are using special characters like german umlaute and stuff in your headings. The servers are not able to deal with something like äöüß.html.

tips_and_tricks/clean_url.txt · Last modified: 2008/06/04 10:57 by till