Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

mod_rewrite to rewrite all incoming filenames

Posted on 2006-04-11
8
Medium Priority
?
193 Views
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!
0
Comment
Question by:FalconFour
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:periwinkle
ID: 16431768
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
 

Author Comment

by:FalconFour
ID: 16432268
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
 
LVL 27

Accepted Solution

by:
caterham_www earned 400 total points
ID: 16434570
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 

Author Comment

by:FalconFour
ID: 16440436
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
 
LVL 15

Expert Comment

by:periwinkle
ID: 16440465
:)  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
 

Author Comment

by:FalconFour
ID: 16440530
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
 
LVL 15

Expert Comment

by:periwinkle
ID: 16440614
^ within a set (which is what the square brackets define) is 'not'.
0
 
LVL 15

Expert Comment

by:periwinkle
ID: 16440628
More information about regular expressions in general can be found here:

http://www.regular-expressions.info/quickstart.html
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
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 …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

581 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