URL Routing for Simple Websites

twitter logo ・1 min read

For a simple website (not Vue, React, non-framework), can you set up a routing system for url's or is it better to store url's within a simple list or array?

Currently I use htaccess file to rewrite urls to remove trailing slashes and file extensions. Is there a better way other than htaccess? FYI, I'm working in php mostly out because of familiarity and includes, but not a MVC setup. A very simple website that's not pulling from a database. Any recommendations are welcome.

twitter logo DISCUSS (4)
markdown guide
 

Hello Cedric, I think you can achieve it using a front controller made up of only .htaccess file and mod_rewrite. But I see you got to use htaccess, till now didn't found any other better way.

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule .*/(.*)$ $1.php?id=$2 [NC,L]

Do check out link

 

thanks, your example is what I have already. The regex is slightly different

#change file extensions
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME}\.htm -f
RewriteRule ^(.*)$ $1.htm

The project I'm working on have about 12 pages but 8 of them are basically the same. Thats why I was considering routing. But I'm not using a DB, the site is really simple. Just wanted to know if the htaccess way the most efficient, or could I create a route config file.

 

Hello there Cedric. Then going by your requirement, I would suggest you happily go for .htaccess way, cause that really seems to be an efficient method here. I would suggest, if possible you can use this small framework Codeigniter, check here link

had some brief experience with Codeigniter back in 2010, but I didn't know a quarter of what I know now. I might give it a look over. Thanks!

Classic DEV Post from Jan 30

Make art, not apps <3

You don't have to build an app. In fact, if you don't need to, then don't.

Cedric W profile image
Frontend Web Developer