apache - 请更多关注我可怜的 mod_rewrite 尝试

标签 apache mod-rewrite httpd.conf hotlinking

我们正在改变用户在博客中嵌入图像的方式,但我需要确保之前嵌入的图像仍然显示。

我正在尝试将旧图像嵌入链接重定向到静态图像或单个旧图像嵌入处理文件。我正在使用 mod_rewrite,但似乎失败了。令人惊讶的是,我已经设法拼凑出一个防止热链接的功能规则集。

不过,我在将旧嵌入重定向到单个旧处理程序时遇到了一些困难。此规则属于热链接集 [L] ,因此仅当嵌入请求不是热链接的结果时才应进行评估。

这是我尝试将所有请求重定向到任何文件,例如 gp[2]?.php[5]?\?p=(.*)

    RewriteRule %{REQUEST_FILENAME} gp[2]?\.php[5]?(.*)=(.*)$ [NC]
    RewriteRule gp[2]?\.php[5]?(.*)=(.*)$ /legacy/gp.php5$1=$2 [L]

但是这不起作用,我只是得到了我精彩的 404 页面。

一些示例请求:

http://ecpod.com/includes/gp.php5?p=cfcfbfbe5d9d63eab4a3e784bd30cfe016fcfcfbfbe5d9d6a19ebc57139d56bb
http://www.ecpod.com/includes/gp.php5?p=928e3888a526b462b684aea5609016f928e3888a585d0963ac4ebb335a522e09
http://www.ecpod.com/includes/gp2.php?p=cfcfbfbe5d9d63eab4a3e784bd30cfe016fcfcfbfbe5d9d6a19ebc57139d56bb

我的目标是让任何看起来像这些示例之一的请求都指向:

http://ecpod.com/legacy/gp.php?p=HASH

感谢您的帮助。

编辑:

旧版网址gp|gp2 , php|php5和一个长度为 n 的查询字符串。它们不一定在 includes 中目录。我知道这很困惑而且很可怕。现在图像嵌入的处理方式有所不同,这种情况永远不会再发生。

日志:

RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5 [NC,L]

192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (3) [perdir /site/http/web/] add path info postfix: /site/http/web/includes -> /site/http/web/includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/includes/gp.php -> includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (3) [perdir /site/http/web/] applying pattern '^/(includes/)?gp2?\.php5?$' to uri 'includes/gp.php'
192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes

RewriteCond %{QUERY_STRING} (^|&)(p=[^&]+)(&|$) [NC]
RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5?%2 [NC,L]

192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (3) [perdir /site/http/web/] add path info postfix: /site/http/web/includes -> /site/http/web/includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/includes/gp.php -> includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (3) [perdir /site/http/web/] applying pattern '^/(includes/)?gp2?\.php5?$' to uri 'includes/gp.php'
192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes


RewriteRule /gp2?\.php5?$ /legacy/gp.php5 [NC,L]

192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (3) [perdir /site/http/web/] add path info postfix: /site/http/web/includes -> /site/http/web/includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/includes/gp.php -> includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'includes/gp.php'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (2) [perdir /site/http/web/] rewrite 'includes/gp.php' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]

外部 <Directory> :

http://www.ecpod-dev.dev/includes/gp.php?p=ff2287e8bb45572e38304afa184ba1a464016fff2287e8bb45572e0606111e7a

RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5 [NC,L]

192.168.1.4 - - [06/Sep/2010:23:46:54 +1200] [www.ecpod-dev.dev/sid#2ac33a5a83d0][rid#2ac34f0c4608/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes

RewriteCond %{QUERY_STRING} (^|&)(p=[^&]+)(&|$) [NC]
RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5?%2 [NC,L]

192.168.1.4 - - [06/Sep/2010:23:47:47 +1200] [www.ecpod-dev.dev/sid#2b02af320470][rid#2b02bcb7b688/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes

RewriteRule /gp2?\.php5?$ /legacy/gp.php5 [NC,L]

192.168.1.4 - - [06/Sep/2010:23:48:45 +1200] [www.ecpod-dev.dev/sid#2aea076a73b0][rid#2aea14f025a8/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes

最佳答案

RewriteRule 测试模式的输入将不包含查询字符串,因此对其进行测试将阻止规则匹配和应用。

考虑到这一点,我们可以将规则修改为如下所示:

RewriteRule ^(includes/)?gp2?\.php5?$ /legacy/gp.php5 [NC,L]

假设查询字符串已经是您想要的格式,我们可以让 mod_rewrite 自动附加它,而无需在重写中指定替换。

如果您不确定查询字符串始终是 p=HASH,并且想要保证它将用于目标脚本,我们可以扩展规则 block 以采用该字符串考虑到,在查询字符串中查找 p 参数并用该部分替换现有查询字符串:

RewriteCond %{QUERY_STRING} (^|&)(p=[^&]+)(&|$) [NC]
RewriteRule ^(includes/)?gp2?\.php5?$ /legacy/gp.php5?%2 [NC,L]

Roland Illig's注释,您还可以执行以下操作以减少对文件所在目录的限制:

RewriteCond %{REQUEST_URI} !^/legacy/
# Input to RewriteRule in .htaccess never has a leading slash, so make sure
# we're at the beginning or there was a slash before the filename
RewriteRule (^|/)gp2?\.php5?$ /legacy/gp.php5 [NC,L]

编辑:更改了规则以反射(reflect)如何在 httpd.conf(而不是 .htaccess)中定义它们。
编辑:...然后又改回来。

关于apache - 请更多关注我可怜的 mod_rewrite 尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632849/

相关文章:

.htaccess mod_rewrite 变量通过重定向

apache - PHP中查询字符串的HTACCESS重写规则

php - Digital Ocean 上 WordPress 的 htaccess 文件持续损坏

apache - 使用 mod_auth_cas 和自定义登录单点登录

linux - 请求点击 Varnish 并获得服务而不检查 mod_security 规则?

node.js - 为什么 apache 重写 URL 两次?

Apache IS_SUBREQ 变量

python - Apache访问日志正则解析

apache - 不,让我们在 ispconfig3 中使用反向代理来加密更新

linux - 我在我的windows上下载了一个网页代码,然后上传到我的linux apache服务器上。为什么会漏图?