Clean URLs with htaccess

I guess I really just don’t understand how the htaccess works. I want clean URLs for better SEO and just so it looks nicer, but every tutorial I read, they just never go into detail of much.They usually just copy/paste some code and tell you that’s how it’s done, but obviously it’s different for everybody.

My site is split up into multiple directories, one of them is herbs, and the url is “”. All of the data is on a database and automatically creates a page when called from the database. So the URLs come out to “/herbs/index.php?herb=lemonbalm” or whatever herb it is. I’d like to turn that into “/herbs/herb/lemonbalm” or even without the second “herb” in that line.

Right now, I have

RewriteEngine On
RewriteRule ^(index)$ $1.php [NC,L]
RewriteRule ^([-\w]+)$ index.php?herb=$1 [NC,L]
RewriteRule ^herbs/([-\w]+)/$ index.php?herb=$1 [NC,L]

All this is doing is turning my url into “/herbs/?herb=herbs”. Please help me on this. It’s driving me crazy!

Try the following:

RewriteEngine On
RewriteRule ^herbs/(.*)$ herbs/index.php

This won’t set the query parameter in the way you’re after, but will get you to herbs/index.php with the original URL set as $_SERVER['REDIRECT_URL']. Now your index.php can split the original URL to get the herb name:

// herbs/index.php

// Convert eg: "/herbs/lemonbalm/" to ["herbs", "lemonbalm"]
$requested_path_parts = explode('/', trim($_SERVER['REDIRECT_URL'], '/'));

if (count($requested_path_parts) == 2) {
    // If we have two parts to our path, we have "herbs" and a herb name
    $page_name = $requested_path_parts[1];
} else {
    // Otherwise, assume we're after the "herbs" index page
    $page_name = 'index';

<h1><?php echo htmlspecialchars($page_name) ?></h1>

Many sites take this approach a step further, and have all URLs redirect to a single index.php that then uses this technique to forward the request to the correct script. This is known as the front controller pattern, and is useful if you want to add sitewide features such as user logins.

Note I’ve left the [NC,L] off your rewrite rule. These flags tell the rewrite engine to ignore letter case when parsing and not follow any other rules if this one matches, respectively. If you need them just add them back on the end.

I’ve always been really weird with my sites. I tend to make everything the hard way, and then start making everything easier. Since I have a lot of data in mysql, I’ve started to create an admin section to be able to edit them through a page like a normal person.

The first part you mentioned wasn’t actually something I had an issue with. I’m good as long as I don’t change the htaccess. I was only getting that issue because of the bad tutorials and probably my misunderstanding of some of the tutorials.

As for the second part, I’ve never been real fond of doing that through php because of all the extra steps, but I guess I gotta take whatever works in certain situations, huh?

So thanks for helping :slight_smile: Maybe I’ll still be able to find someone to help me with the htaccess part, though.

Anybody else have any ideas, please?

Sponsor our Newsletter | Privacy Policy | Terms of Service