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!

    19 days later

    Well, does it work? That's a valid question; if it does, it's certainly worth pursuing.

    Is there a better way? That's another. It occurs to me that a ForceType directive might accomplish the same thing; but I'd need to do a bit of research to see if one approach had any advantages over the other.

    In terms of your code, would [man]parse_url/man save you some overhead and hair pulling with all those array calls?

      Yup, it works.

      I don't think parse_url() would streamline anything in this case. Am I wrong? Can you get a little more specific?

      Thanks!

        Write a Reply...