mod_rewrite to rewrite all incoming filenames

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!
FalconFourAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

periwinkleCommented:
Here's a good place to start:

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

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".

HTH!
0
FalconFourAuthor Commented:
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... :-(
0
caterham_wwwCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

FalconFourAuthor Commented:
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!
0
periwinkleCommented:
:)  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
0
FalconFourAuthor Commented:
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?
0
periwinkleCommented:
^ within a set (which is what the square brackets define) is 'not'.
0
periwinkleCommented:
More information about regular expressions in general can be found here:

http://www.regular-expressions.info/quickstart.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.