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).

cmb: see the HowTo for CMSimple_XH in the CMSimple_XH-Forum.

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.

{{{{———-
NGINX instead of apache

If You are using NGINX instead of apache, You need to have access to the configuration file of nginx since nginx does not support .htaccess files. Due to the flexibility of NGINX' rewrite module the rule itself is simpler:

if (!-e $request_filename) {
    rewrite  ^/(.*).\html$  /index.php?$1  last;
    break;
}

Reload the config file after the change by simply

killall -HUP ngninx

———-}}}}

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 like that in your headings. The servers are not able to deal with something like äöüß.html.

~~DISCUSSION~~

tips_and_tricks/clean_url.txt · Last modified: 2018/04/05 14:48 (external edit)
Webdesign: NMuD chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0