Johan Broddfelt
/* Comments on code */

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
<?php
class Post extends Db {
    public $table = 'post';

    function linkTitle() {
        return str_replace(' ', '_', mb_strtolower($this->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
<div>
    <?php
    $post = new Post();
    $list = $post->fetchArray('WHERE publish < '' . date('Y-m-d H:i:s') . '' ORDER BY publish DESC LIMIT 20');
    foreach ($list as $item) {
        ?>
        <a href="<?php echo $item-linkTitle(); ?>-<?php echo $item->id; ?>">
            <div class="sub_menu_title"><?php echo $item->title; ?></div>
            <div class="sub_menu_info"><?php echo $item->tags; ?> - <?php echo $item->posted; ?></div>
        </a>
        <?php 
    }
    ?>
</div>

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
        <nav id="menu">
            <div id="small_menu" class="small_menu">
                <a href="" id="main_menu" class="right"><span class="typcn typcn-th-menu"></span></a>
            </div>
            <div id="large_menu" class="large_menu">
                <a href="/">Home</a>
                <a href="" id="post_link">Posts</a>
                <!--<a href="">Tags</a>-->
                <a href="links-page">Links</a>
                <a href="about-page">About</a>
                <?php if ($ad->user->id > 0) { ?>
                    <a href="?user=logout" class="right">Logout</a>
                <?php } else { ?>
                    <a href="index.php?module=user&view=login" class="right">Login</a>
                <?php } ?>
            </div>
        </nav>
        <nav id="sub_menu_post" style="display: none;"><?php include('views/post/post_menu.php'); ?></nav>

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

Follow using RSS

<< Build a mailinglist Send mail on new comments >>

Comment

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