Johan Broddfelt

SEO friendly urls

The most obvious way to manipulate the url is in the .htaccess file in the root of the site. Most of the sites about SEO friendly url:s, suggest that you should use links like http://url.com/page/12/my_page_title. But since I prefere relative links to resourses such as css, javascript and images. I do not get those resourses loaded when using that kind of link. So for my posts I want to use http://url.com/my_title-12 instead for mapping against index.php?module=post&view=item&id=12 and http://url.com/my_title-page for general pages. Here is how I solve that in my .htaccess file.

// .htaccess
RewriteEngine on
RewriteRule ^([^/]+)-([0-9]+) index.php?module=post&view=item&id=$2 [NC,L]
RewriteRule ^([^/]+)-([^/]+) index.php?module=$2&view=$1 [NC,L]
#RewriteRule ^([^/]+)-page index.php?module=page&view=$1 [NC,L]
#RewriteRule ^([^/]+)-subscription index.php?module=page&view=$1 [NC,L]

# NC makes the rule non case sensitive
# L makes this the last rule that this specific condition will match

Now I need to update the links in my menu. Notice that we load the title and change it to lower case and then repace all spaces with _ before we add the id to the end of the url. For convinience I will create a function in the Post class to handle this.

// classes/post.php
title));
    }
}

Then I update the menu with a call to our new function. Now I can reuse this function in the items.php and mailinglist.php as well. And if I for some reason want to modify the text that is returned by this function I could change it in one place.

// view/post/post_menu.php
fetchArray('WHERE publish < '' . date('Y-m-d H:i:s') . '' ORDER BY publish DESC LIMIT 20'); foreach ($list as $item) { ?>

We aso need to update our main menu. Here just type the new link instead of the long one: links-page and about-page.

// view/start/menu.php
        
        

You may notice quite long load time the first time you load a page using the new rule. But after that the server should know how to do the rewriting.
There is one thing about this way of manipulating the url:s and that is that if I think of a better way of rewriting urls in the future that is conflicting with this way, then there will be a lot of links out there that is not pointing to the correct page any more. But on the other hand having good links is probably better then having a bad link even if the future might present a better way to do this.

- framework, php, url, software

<< Build a mailinglist
Follow using RSS
Send mail on new comments >>

Comment

Name
Mail (Not public)
Send mail updates on new comments
0 comment