开发者

Apache Alias "Best guess" filename

Using Apache and mod_rewrite I can rewrite a complex request to a simple filename, eg:

RewriteRule ^shortcut/(.*)$ /long/way/around/$1

Can this work in reverse? I want a simple request to be rewritten to an unknown file, but I can identify which file should be served by a unique ID number prefixed to it's filename. I want Apache to "guess" using a regular expression which file to serve based on the ID.

For example:

GET /img/29281.jpg

Directory of /img/:
...
29280-filename-here.jpg
29281-other-filename-here.jpg  <-- Apache should serve this one
29282-more-files-here.jpg
...

So, a regular expression rewrite could perhaps be:

^(\d+)\.jpg$   -->   ^$1\-[a-zA-Z0-9-_]+.jpg$

How to integrate this into Apache (if it's possible)?

Many thanks for any suggestions.

P.S. Renaming all the filenames to the simple ID numbe开发者_如何学Pythonr isn't an option in this instance.


I think one of the apache-way is to use mod-rewrite with the RewriteMap directive, and using the :prg mapping of the rewritemap.

This will load at apache start a simple program (may be a perl one as suggested in documentation, a PHP program may get more memleaks & performance problems, IMHO). This program will have to do the last part of the filename guess.

You can find some examples of such programs here (PHP), there (C), and here a Perl basic one.

I'm not enough perl-enabled in my brain to get the perl way of extracting real filename taking the extension and the int identifier but that should be easy and short (which is important for a PRG rewriteMap).


You could use a PHP script as an ErrorDocument for the 404 page. This script would find the desired file, and send a redirection to the wanted file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜