如何同时使用 apache mod_rewrite 和 alias?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/334111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:59:08  来源:igfitidea点击:

How to use apache mod_rewrite and alias at the same time?

apachemod-rewritealias

提问by p.g.l.hall

I have a directory outside the webroot that is used for storing images uploaded from a separate admin system. Images are stored in this format:

我在 webroot 之外有一个目录,用于存储从单独的管理系统上传的图像。图像以这种格式存储:

filepath/writable/images/00/00/23/65/filename-236581.jpg

(where the webroot is filepath/html)

(其中 webroot 是文件路径/html)

...for example. Here, 236 is the ID of the image in the database, and the file system is broken into numbered directories to stop more than 100 inodes from being used within one directory.

...例如。这里,236是数据库中镜像的ID,文件系统被分成编号的目录,以防止在一个目录内使用超过100个inode。

I want to be able to access these from the front-end webserver, like this:

我希望能够从前端网络服务器访问这些,如下所示:

http://(server)/filename-236581.jpg

Where filename is an seo-optimised string, the same as on the name of the actual file.

其中 filename 是一个 seo 优化的字符串,与实际文件的名称相同。

I can get mod-rewrite to rewrite the URL so that it contains the extra numbered directories, and I can get alias to redirect the request to the writable/images directory, but I can't do both at the same time. If I put both the alias and mod_rewrite directives in, it ignores the alias one and the error log complains that it can't find filepath/html/uploaded-images.

我可以使用 mod-rewrite 来重写 URL,以便它包含额外编号的目录,并且我可以使用别名将请求重定向到 writable/images 目录,但我不能同时执行这两项操作。如果我同时放入 alias 和 mod_rewrite 指令,它会忽略别名,并且错误日志抱怨它找不到 filepath/html/uploaded-images。

Here is what I have so far:

这是我到目前为止所拥有的:

RewriteRule ^(.*)\/([^\/]*)-([0-9])\.(gif|jpg|jpeg|png)$ /uploaded-images/00/00/00/00/-. [L,NC]

...and so on, all the way up to:

...等等,一直到:

RewriteRule ^(.*)\/([^\/]*)-([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])\.(gif|jpg|jpeg|png)$ /uploaded-images/////-. [L,NC]

alias /uploaded-images "filepath/writable/images"

Removing the [L] makes no difference.

删除 [L] 没有区别。

回答by Martin

The [L] means last.

[L] 表示最后。

What you really need to use as well is the [PT] flag to indicate that the resultant path from your rewrite rule should be passed through to next handler which in your case is the alias directive.

您真正需要使用的是 [PT] 标志,以指示重写规则的结果路径应传递到下一个处理程序,在您的情况下是别名指令。

As the [PT] tag also implies the [L] tag you no longer require [L] aswell.

由于 [PT] 标签也暗示了 [L] 标签,因此您不再需要 [L] 标签。

回答by adam

That [L] you have after the RewriteRule means "Last"

在 RewriteRule 之后的 [L] 表示“最后”

To be honest, I believed this only meant further RewriteRule lines would be ignored, but it might be worth checking out.

老实说,我认为这仅意味着更多的 RewriteRule 行将被忽略,但它可能值得一试。