SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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.
    Code:
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!)

  4. #4
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    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.

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    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)*

  7. #7
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Aw, Mallory!

    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).
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •