View Full Version : Friendly URL's without mod_rewrite?

11-14-2006, 09:10 AM
Hi all :)

As you know SEF (Search Engine Friendly) URL's are becoming more popular daily. The easiest way make it is .htacces, but it needs mod_rewrite to be enabled on server and it's a big problem for many of host providers.

Is there any other ways to make friendly URL's? :confused:

Thanks, Otto.

11-14-2006, 11:19 AM
Yes of course there is.

Basically, you need to specify a 404 NOT Found error page. If you are running on Apache....then do the following in a .htaccess file

ErrorDocument 404 /internal_handle/url.php

Note that we have a folder called "internal_handle" and within that folder...we defined another url.php which would take care of 404 NOT Found errors.

the following is a sample of how you can handle a NOT FOUND URL in the url.php file.

$pages = explode("/",$_SERVER['REQUEST_URI']);

echo "<h4>Requested URL Broken Down</h4>";

foreach( $pages as $k => $v ) {
echo $k . ": " . $v . "<br/>";
Go <a href="/">Back</a>

I have also create an index.php file for you to experiment with. all it contains is just a sample of links. here it goes..

<li><a href="/first_page/">first page</a></li>
<li><a href="/second_page/">second page</a></li>
<li><a href="/first_page/child_page">first page | child page</a></li>
<li><a href="/second_page/child_page/">second page | child page</a></li>
<li><a href="/another page/">another page</a></li>

Let's know if you need more help.


11-14-2006, 12:21 PM
For more explanation on ess' post, see http://www.zend.com/zend/art/scriptcaching.php

11-14-2006, 01:14 PM
Hmm... Never hared about it before. Thanks guys :)

11-14-2006, 04:39 PM
Alternatively (and not requiring .htaccess at all) you can use the server variable $_SERVER['PATH_INFO'];

e.g with a url.. http.....blah.com/index.htm/section1/title1

$_SERVER['PATH_INFO'] will contain `/section1/title1`, explode that

$bits = explode('/', $_SERVER['PATH_INFO'] );

you can then use $bits[1] and/or $bits[2]/3/4/5 whatever to query your DB or server up the appropriate page.

This all assumes a standard apache configuration ... try it and see
Note though that there are issues related to this , with a url of ...index.htm/var1/var2 then paths (for images/links etc) are relative to /index/var1/var2 NOT /index.htm e,g with an 'images' folder at the same level as index.htm you would have to call `../../images/img.jpg` rather than the expected `./images/img.jpg`

11-14-2006, 06:24 PM
Alternatively (and not requiring .htaccess at all) you can use the server variable $_SERVER['PATH_INFO'];

I am not sure you can do that without htaccess OR without access to httpd.confg.

That is, when Apache catch a 404 error...it would look for a customized definition for 404 page. If none are found...it would use the default (the defaults vary depending on your distro).

By the way...you can also use the script provided above with IIS...though some changes are in order.

Instead of using $_SERVER['REQUEST_URI']...(IIS wouldn't have clue what you are saying here) you need to use $_SERVER['PHP_SELF']...or use $_SERVER['PATH_INFO'].


11-14-2006, 06:47 PM
the default apache1.* behavior is not to use a custom 404 (depending on flavour), thats setup in the httpd.conf or .htaccess , AFAIK the PATH_INFO and lookback features have to be disabled rather than enabled.

That said almost all hosts have setup or allow custom 404's so there is nothing wrong with that approach , my effort was just another method to think about (and the one I personally prefer)