Apache Alias “Best guess” filename

醉酒当歌 提交于 2019-12-22 10:49:51

问题


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 number isn't an option in this instance.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/5865529/apache-alias-best-guess-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!