本文介绍了Apache的别名"最佳猜测"文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Apache mod_rewrite的和我可以重写一个简单的文件名的复杂要求,例如:

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

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

可这工作反转?我想要一个简单的请求被重写一个未知的文件,但我能确定哪些文件应该由pfixed到它的文件名唯一的ID号$ P $送达。我想Apache来猜测用来发球根据识别哪个文件定期EX pression。

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.

例如:

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

那么,一个普通的前pression重写或许是:

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

任何建议非常感谢。

P.S。重命名所有的文件名,以简单的ID号不是在这种情况下的一个选项。

P.S. Renaming all the filenames to the simple ID number isn't an option in this instance.

推荐答案

我想的Apache的方法之一是使用国防部重写用的指令,并使用:PRG 的RewriteMap指令的映射

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

这将在阿帕奇加载启动一个简单的程序(可能是一个perl一个作为在文档中建议,一个PHP程序可能会获得更多的memleaks&放大器;性能问题,恕我直言)。这一计划将要做的文件名猜测的最后一部分。

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.

我还没有在我的大脑得到真正的提取以文件名扩展和INT标识符,但应该是很容易和短(这是一个PRG RewriteMap指令重要)的perl的方式使Perl的。

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

这篇关于Apache的别名&QUOT;最佳猜测&QUOT;文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:39