Organized, Short Folder Structure Using mod_rewrite

By  on  

I tend to be over-organized when I create the folder structure of my customers' websites. I like using descriptive file and folder names for a number of reasons:

  • Search engine friendly URLS and image names (for image searches)
  • Descriptive file names give users a "preview" of the linked page in the status bar (if the anchor text isn't descriptive enough or the link is an image)
  • I want to be able to tell what a file is when I see the file name

One problem with my method is that long folder names can be annoying to type and can bloat your pages resulting in ugly load times. I use the power of mod_rewrite to shorten paths to files I reference most, image files.

The XHTML

Image

becomes...

Image

The mod_rewrite .htaccess

RewriteEngine On
RewriteRule ^gis/(.*)$ /graphics/icons/small/$1 [L]

Recent Features

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    Create a Dynamic Table of Contents Using MooTools 1.2

    You've probably noticed that I shy away from writing really long articles. Here are a few reasons why: Most site visitors are coming from Google and just want a straight to the point, bail-me-out ASAP answer to a question. I've noticed that I have a hard time...

  • By
    jQuery Comment Preview

    I released a MooTools comment preview script yesterday and got numerous requests for a jQuery version. Ask and you shall receive! I'll use the exact same CSS and HTML as yesterday. The XHTML The CSS The jQuery JavaScript On the keypress and blur events, we validate and...

Discussion

  1. Very slick :)

  2. Okay very interesting. How would that apply if your directory structure was set out like this?

    /nr/rdonlyres/2069656a-f68e-4563-b9a5-4b9a9922f7a4/0/transferform0809.pdf

    /nr/rdonlyres/86e6a9ab-1633-4e4b-8790-bff74df03487/0/lamslistpublishedweb11november08.pdf

    As you can see the 3rd subfolder changes and could be anything.

    What if I wanted it to look like.

    /attachment/filename.pdf

    Using the method you mentioned how would it work?

    Cheers
    zeroanarchy

    • efwf

      hahaha melllttttttttt , no one likes you !

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!