Allowing for incorrent/missing file extension

I was wondering if there is a mod rewrite function that can allow for users to use URLs without the correct file extension. e.g. …example.com/page.php could also be found by typing …example.com/page.

It’s not a huge issue, obviously, but I thought that it would help users a little and reduce 404s. I have not seen this exact scenario covered in any htaccess tutorials.

Aw, Mallory! :blush:

That was NOT a personal attack! I’m sure you know that but others may not.

Besides, a good webmaster will always TRY to prevent the NEW, IMPROVED ID-ten-T from making mistakes on their site(s). :lol:

As an aside, my fingers are often dyslexic as I try to keep them flying on the keyboard! I spend more time correcting typos than typing in the first place!

Regards,

DK

to protect against the better idiots out there who avoid your links in favor of typing their own URIs for your website

Stomme poes sheepishly admits to typing in URLs directly fairly often, and not always getting it right (or fatfingering it)

Mallory,

The OP’s example was example.com/page so I made the assumption that, like my website, he/she was looking for extensionless URIs. Your version is a better substitute if you want to protect against the better idiots out there who avoid your links in favor of typing their own URIs for your website - just be sure to omit the REAL extension from the list of options!

If you do that for images, I think you may be in trouble unless you use the RewriteCond %{REQUEST_FILENAME}.jpg !-f (and same for .gif, .png, .yadda-yadda). before redirecting to the specific file extension. Lacking the correct extension, the file’s MIME type will not be transmitted so the image will not likely be handled correctly.

Regards,

DK

adh,

Hmmm, good question. Yes, there is (I use it at my website).

  1. The first thing to do is check that you have mod_rewrite enabled and not in comment mode (RewriteEngine on).

  2. Check that the request is not a directory (or a file) (RewriteCond %{REQUEST_FILENAME} !-d then repeat for !-f).

  3. Redirect if extensionless (no dot character in the file - I’m simplifying to say no dot character in the path/file) with RewriteRule ^([^.]+)$ $1.php [L].

Note that all the extensionless file requests are being redirected to the php version of that filename. Obviously, if you’re using .html, use .html instead.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [L]

WARNING! If this gives you a 500 error, you do not have mod_rewrite enabled on your server!

Caution! If this redirection results in a 404, your 404 script will identify that the php’d version of the request does not exist, not the extensionless version.

Regards,

DK

Redirect if extensionless (no dot character in the file - I’m simplifying to say no dot character in the path/file) with RewriteRule ^([^.]+)$ $1.php [L].

Hm, but I thought the requirements also meant, if someone meant to grab page.php, but typed in not only page (taken care of) but maybe they type in
page.html
or
page.htm
or
page.shtml
or whatever.

Would is be worth it to look for a list of probable (optional) endings
^([^.]+)(\.s?html?|\.php)?$
either ending in any of those, OR no dot (protect images etc)?

I’m a retarded typist, get 404s all the time because the l doesn’t make it through and I only have .html files, not .htm… have been thinking of rewriting that just for my fatfingerdness.

Hi Stomme; thanks for commenting.

I had thought of that problem, so I already have html-derived file extensions covered by a 301 rewrite rule that I got from, if I remember right, corz.org. I just needed to accommodate a potential lack of any extension.

Thanks! I’ll give that a go.

(Definitely have mod rewrite enabled, BTW. There’s an ever growing list of other stuff in my htaccess that uses it!)