intelliproject logo

Location: Web development - PHP    License: The GNU General Public License (GPL)

Friendly URLs without mod_rewrite

Posted by Giulio Bai

For those who don't want to mess with Apache stuff, here's a quick alternative to mod_rewrite usage

Skill: Beginner

Posted: 05/12/2008

Views: 678

Rating: 0.00 /5

Popularity: 0.00

Sign Up to vote for this article

Introduction

Obtaining nice-looking, search engine-friendly URLs is not that easy, especially when dealing with dynamic content.
A common way to overcome the problem is to use the Apache mod_rewrite. But for those who haven't that particular web server installed or don't want to mess with it (yes, it's not that strightforward as it sounds), here's the quick alternative!

The problem

So, we have our nice webpage online at http://example.com/index.php?group=projects&name=myproject&page=7, but we're not happy with it: quite awful uh?

We'd prefer something like http://example.com/index.php/projects/myproject/7

A possible solution

Here we go!

What the above code does

Firstly, we get the current document URL stored in the $_SERVER array. We then remove the index.php token, replacing it with... well, nothing (''). The last step is to explode the string so that the stuff we look for ('projects', 'myproject', '7') is all put into our array $vars

Congrats, you have clean URLs now!

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPL)

About the author

Giulio Bai

Location: Italy
Home page: http://giulio.hewle.com

Posted by Constantin Botnari at 06/12/2008 01:55
but if I will remove the index.php from this url http://example.com/index.php/projects/myproject/7 the apache will give an 404 Error :) we can try something like that http://example.com/?/projects/myproject/7 but also is not so friendly like an url http://example.com/projects/myproject/7 with mod_rewrite :)
Posted by Giulio Bai at 06/12/2008 05:17
As shown in the example, the index.php part must not be removed.

And yes, of course this method is different from mod_rewrite usage.
Posted by Ahmed Magdy Ezzeldin at 07/12/2008 12:25
Well this idea is cool but there is something missing before you can use url redirection with this script. This is because when you access a page that is not found it will give a 404 Error.

On Servers that do not support mod_rewrite like lighttpd for example you can set your 404 Error page to be your index.php page. So whenever you access a url that is not found the server redirects to your index.php that parses the request and does the magic.

Sign up to post message on the article message board!