Clean URLs - Internal Server Error with slash

I searched all afternoon so thought it was time to see if anyone knows how I can do this.

I want to have a clean URL such as www.domain.com/thingy be redirected to www.domain.com/thingy.php, but still display the former in the address bar.

I am using the following htaccess to remove the file extension:

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME}\\.php -f
RewriteRule ^(.*)$ $1.php

This works fine but if someone were to add a trailing slash to the URL such as www.domain.com/thingy/, it results in a 500 error. How can I change the htaccess file so that it will produce a 404 instead (as I have seen on other sites)? It’s probably OCD but I just don’t like the 500.

Another related question, if the htaccess file were to allow the URL with or without the trailing slash (which is what I had initially), is this regarded as duplicate content by Google?

Thanks for any help.

red,

I understand your problem as I DO use extensionless URIs at my website (using RewriteRule ^([^./]+)$ $1.php [L] which is limited to the DirectoryRoot by the NOT /).

Trailing /? Why include links like that? Argh! That affects both the mod_rewrite AND the relative links in the served file! Okay, rant: off! Use /? after the {REQUEST_FILENAME} and before the \.php in the second RewriteCond.

While I doubt that would be picked up by SE’s you may want to ask that in the SEO forum board.

Regards,

DK

Hi DK

Thanks for that. I changed the rewrite rule to the one you suggested and this causes a 404 error when adding the trailing slash (which is what I wanted).

I don’t want to have links with a trailing slash, I was just trying to produce a 404 in case anyone ever links to one of the pages incorrectly by adding a trailing slash (as people often expect there to be one).

I just had a quick click through on your site and your mod rewrite page is actually in my bookmarks from when I was first learning about it (found through google), small world! Very useful page so thanks for that too!

red,

The code I use (as posted above) does NOT accommodate the (optional) trailing /. For that to work without the 404 (which is NOT what you want?), you’ll have to use RewriteRule ^([^./]+)/?$ $1.php [L] AND use absolute URLs in your $1.php script (because of the different directory levels perceived by the browser for the / and non-/ URIs).

Regards,

DK

It’s ok, I do not want to allow for the trailing slash, I just wanted it to 404 when adding the slash rather than 500, which it now does.