Hello everyone. I'm new to this forum and hoping you guys can give me some feedback.
I'm making a relatively small non-CMS based site and would like to use clean URLs in the form of:
mysite.com/home
mysite.com/therapy
mysite.com/therapy/acupuncture
etc.
I've used mod_rewrite to send all requests to /index.php which contains the following working code. Is there a better way I could do this? Is there a glaring problem with the way I've done it?
This is my first time using mod_rewrite and clean_urls so I'd like to be sure I haven't messed something up without knowing.
<?php
// Sanatize the request and put its parts into an array called $url_array
$url_request = strip_tags($_SERVER['REQUEST_URI']);
$url_array = explode("/", $url_request);
// Clean $url_array by removing empty elements
// The first element is always empty so let's shift it off
array_shift($url_array);
// If the last element is also empty then pop it off
if(end($url_array) == ""){
array_pop($url_array);
}
// if $url_array is empty, show home and exit this script
if (empty($url_array)){
include("home.html");
exit();
}
$filename = implode("-", $url_array) . ".html";
if ( file_exists($filename) ) {
// file exists, include it
include($filename);
exit();
} else {
// file does not exist, should return a 404
// header('HTTP/1.0 404 Not Found');
exit("<h1>404 Not Found</h1>\nThe page that you have requested could not be found. You may like to start over from <a href='/'>Home</a>");
}
?>
My .htaccess file looks like this:
Options -MultiViews
ErrorDocument 404 /404.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
And just to be 100% sure, should all the hard-coded links look like <a href="/therapy/acupunture">Accupunture</a> throughout the site?
Thanks for any feedback!