mod_rewrite to rewrite all incoming filenames

Posted on 2006-04-11
Last Modified: 2010-03-04
I'm used to using IIS for everything, but I'm forced to move to Apache because IIS doesn't support URL rewriting like Apache does. It's sad to give it up, but I just can't stand the system I'm using now.

What I'm using right now is a folder for each filename, with an "index.php" file inside each folder. The actual file is stored under "/data/". I use the PHP file to call the hosting-include file, which serves up the file as if index.php were the file itself - but it also allows me to monitor file progress, popularity, and redirect the client to a new offsite URL if I want to.

I'm testing out Apache on my desktop with a dummy structure, as my server is still running the old IIS setup. Hoping to get everything set up right the first time, for a smooth and effortless transition. Yeah right! :-P

Basically, what I need to do is create a mod_rewrite setup that if an incoming file doesn't exist in the root folder, the filename is sent to file.php?file={request-filename}. From there, file.php will determine whether or not the file actually exists, and will perform the file-serving.

If the requested file *does* exist, though (like file.php itself, as well as several support files and images), it should pass through like a normal request.

I'd prefer to learn how to do this myself, because regular expressions confuse the heck out of me, so if someone could explain every little insignificant character of the regular expression used in their answer, I'd really appreciate it!

p.s.: This is my first question - hope I'm doing it right!
Question by:FalconFour
    LVL 15

    Expert Comment

    Here's a good place to start:

    As well as teaching you in general about mod_rewrite, it also has an example of how to "Search pages in more than one directory".


    Author Comment

    Yeah, I tried that guide, but it wasn't so much a "guide" as a "here are some scripts to do things that I can't even explain". That's one of the pages I visited that gave me a headache. It was really not very helpful... :-(
    LVL 27

    Accepted Solution

    an incoming file doesn't exist in the root folder, the filename is sent to file.php?file={request-filename}

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

    Order of processing is:
    regEx ^([^/]+)$ matches every request of 1 or n characters not containing a slash. If that is true, the condition checks, if the file does not exist. If that is true, too, the substitution will be applied.

    You can place the ruleset into your httpd.conf, in your <Directory...>-section for your htdocs filder, not(!) <Directory />, but e.g. inside of a <Directory "C:/path/to/htdocs">.....</Directory> section.

    Author Comment

    That worked great! Exactly what I wanted. Thanks! I could use a bit more explanation of what all the characters meant, but I imagine that's got to be as hard as me explaining PHP to a noob (thinking "what do you mean, what's the $ in a variable for?"). So I won't hold it against ya. It works, and that's what counts.

    Thanks again!
    LVL 15

    Expert Comment

    :)  As a bit more info:

    ^ matches beginning of line
    $ matches end of line

    Anything in ()'s can be referred to as $1 (for the first parens) $2 (for the next if there is one), etc.

    + matches one or more times
    * matches zero or more times

    Author Comment

    Crap. I wish I could give you some extra points for that, but I don't seem to be able to :-(

    Thanks!! That cleared up just about all my questions except for how caterham_www managed to exclude slashes using [^/]... how's that work?
    LVL 15

    Expert Comment

    ^ within a set (which is what the square brackets define) is 'not'.
    LVL 15

    Expert Comment

    More information about regular expressions in general can be found here:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Suggested Solutions

    Title # Comments Views Activity
    External Wordpress Site loads very slow 1 190
    Xampp Not Stopping 9 30
    apache and php 3 80
    Redirect and Rewrite URL using .htaccess 38 71
    If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
    In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now