Friendly URLs

Hi,

Is there a way in PHP that I can make the URLs more friendly without the PHP extension?

You will need to use mod_rewrite (which is an Apache extension, not a PHP extension).

There’s also the option of using PATH_INFO, but I don’t know much about that variant.

Without either of those two options, the best you could do is to use your site’s folder structure to empart semantic meaning to your URLs.

As immerse said, the best way is mod_rewrite on apache servers.

Chances are it will probably already be enabled, and you can put a .htaccess file into your document root. In this you can define rules for your pages.

For example
site.com/contact
points to
site.com/contact.php

There’s actually a blog on sitepoint about it
mod_rewrite: A Beginner’s Guide to URL Rewriting Article » SitePoint

Here is a clip of the htaccess I use on my site…


Options +FollowSymLinks
RewriteEngine On

ErrorDocument 404 /404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^example/([^/]+)/?$ /index.php?doc=$1 [L]

RewriteRule ^([0-9]+)/?$ index.php?num=$1 [L]
RewriteRule ^([A-Za-z_]+)/?$ index.php?letters=$1 [L]

should grab url.com/213/ and the server would see it as index.php?num=213
vs url.com/abc/ the server would see as index.php?letters=abc

and if someone was to visit url.com/23bae or something random it should be picked up by the 404 page.

vs url.com/example/this would become index.php?doc=example

I haven’t worked with htaccess for a while, but this was what I came up with last time I meddled with it… If anything is wrong maybe someone else can clarify.