Extensionless URLs for plain PHP site

Hi there

I know how to do set it up in the .htaccess file for a WordPress installation, but how would one go about creating extensionless URLs for a plain PHP site?

Some things you might need to know:

  • All pages (currently) reside in the root folder
  • So each page has a name like about-us.php or contact-us.php

Thanks in advance.

You could do it via the .htaccess file on a normal HP site too, but another option to consider is to rename each page like contact-us.php to index.php and place it in a folder called /contact-us/. That way, your URLs will look like mysite.com/contact-us/.

Hi Ralph.

Thanks for the speedy reply.

So you mean I can use the same mod-rewrite function as one would in WordPress:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

And then just place each index.php file into it’s own folder, like /contact-us/ ?


No those are two different things. Creating a folder /contact-us/ and creating an index.php in there will work without any .htaccess. Simply because index.php is (on most servers) the default file to load if no filename was supplied. Think about [noparse]http://www.yourdomain.com/[/noparse], also supplies [noparse]http://www.yourdomain.com/index.php[/noparse], same thing goes for folders. That’s the first and easiest option.

The second option is to use .htaccess for explicit rewrite of your URLs to your php files like so:

RewriteEngine On
RewriteRule ^contact-us$ contact-us.php [L]
RewriteRule ^about-us$ about-us.php [L]

This will display contact-us.php when you request /contact-us, and about-us.php when you request /about-us

The third option is to make that a little bit more generic like so

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\\.php -f
RewriteRule ^(.*)$ $1.php [L]

This will take the URL supplied and see if there is a file with that URL with .php glued after it. So, if you request /contact-us, it will look if there is a /contact-us.php, and if there is it will serve that file.

Then the forth option is the most powerful (IMHO) and basically the one WordPress uses

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php [L]

What this does it will send all requests for non-existing files and folders to your index.php so you can handle the request over there.

A real basic setup would be something like this

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

if ($url[1] == 'about-us')
   // show the about us page
else if ($url[1] == 'contact-us')
   // show the contact us page

obviously you won’t put all the code for the pages inline there, so you will probably want to [fphp]include[/fphp] them, or create controllers to go for a MVC approach; but that’s getting a bit out of the scope of this thread.

There are other options, but they’re not as nice as the once described above.

Hope that helps :slight_smile:

Hi Scallio

Thank you for the in-depth reply - I really appreciate it.

I’ll check it out and see which works best for my situation.