htaccess的:重定向图像一个页面,显示图像 [英] htaccess: redirect images to a page that shows the images

查看:202
本文介绍了htaccess的:重定向图像一个页面,显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想所有图像重定向到一个showimage.php,使用的htaccess

I would like to redirect all images to a showimage.php, using htaccess

但是,如果我尝试像

RewriteRule ^images/([A-Za-z1-9]+).png$ showimage.php?image=$1 [L]

要显示的图像上该网页会被重定向太(DOH)

the image to be shown on that page will get redirected too (doh)

如何重定向所有图像,但不会如果他们是从showimage.php要求? (或从该服务器)

How to redirect all images, but not if they are requested from showimage.php? (or from this server)

推荐答案

有两件事情可以做:

-1-没有重定向如果查询来自服务器
你只要有重写规则,它说,下面的重写规则是不是要做如果HTTP查询的来源是这样的服务器之前添加的RewriteCond指令。

1- no redirect if the query comes from the server You just have to add a RewriteCond directive before the RewriteRule, which says that the following RewriteRule is not to be done if the source of the http query is this server.

RewriteCond %{REMOTE_ADDR} !127.0.0.1

根据您的脚本请问该怎么弄,你可能需要改为使用你的服务器的私有IP。或添加其他的RewriteCond这个IP,以prevent都被重定向。

Depending on how your script does the get, you may need to use instead your server's private IP. Or add another RewriteCond with this IP, to prevent both to be redirected.

2 - 你也可以根据查询的引用者prevent重定向,但你必须确保它与查询发送。 (引用者是请求您的网址源URL。例如,如果你在 HTTP://localhost/index.php ,然后点击一个链接到 HTTP://localhost/help.php ,最后一个的引用者会 HTTP://localhost/index.php )。
你仍然使用的RewriteCond,但HTTP_REFERER,而不是REMOTEHOST。

2- you can also prevent redirect depending on the referer of the query, but you have to be sure that it is sent with the query. (referer is the source URL that requested your URL. For example, if you're on http://localhost/index.php and click a link to http://localhost/help.php, the referer of the last one will be http://localhost/index.php). You still use a RewriteCond, but with HTTP_REFERER instead of REMOTEHOST.

例如:

的RewriteCond%{HTTP_REFERER}!showimage.php线

RewriteCond %{HTTP_REFERER} !/images/.*\.png

您可能不得不玩具位与完整引用者,而不是用户,如果这是不够的,但刚开始的想法。
您可以检查哪些引荐你在访问日志(如果你有%{Referer的}​​我在您使用的LogFormat启用)

You may have to toy a bit with the full referer, not user if this is enough, but just the idea to start with. You can check which referer you have in the access log (if you have %{Referer}i enabled in the LogFormat you use)

明白了:问题是,引用者是被要求的URL,而不是重写一(应该我已经想过这个问题,很明显)。那么,问题是,当你需要 HTTP://localhost/images/x.png ,它是改写为 HTTP://localhost/showimage.php图像= x.png 供应与内部的页面。但是引用者是不是showimage.php但... /影像/ x.png(初始URL)。因此,在HTTP_REFERER的的RewriteCond必须检查源URL是一样的东西,而不是/images/*.png。

Got it : problem was that, the REFERER is the URL that was asked, and not the rewritten one (should I've thought about it, it's obvious). So, the problem is that when you require http://localhost/images/x.png, it is rewritten as http://localhost/showimage.php?image=x.png which serves the page with the inside. BUT the referer is NOT showimage.php but .../images/x.png (the initial URL). So, the RewriteCond on the HTTP_REFERER must check that the origin URL is something like /images/*.png instead.

这是我的工作的例子。规则是在[DOCROOT] / htaccess文件:

Here is my working example. Rules are in [DOCROOT]/.htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !/images/.*\.png
RewriteRule ^images/([A-Za-z1-9-]+).png$ image.php?name=$1 [L]

下面是我的image.php文件:

Here is my image.php file:

<html>
Here it is : <img src="/images/<?php echo $_GET['name']; ?>.png"/>
</html>

使用我的浏览器,要求HTTP:// [服务器的IP] /images/foo.png会显示一个HTML页面,包含这是:和请求的图片,从[DOCROOT]拍摄/影像/ foo.png。

With my browser, calling http://[server ip]/images/foo.png will display an html page, containing the words "Here it is : " AND the requested picture, taken from [DOCROOT]/images/foo.png.

对于与此服务器工作,你只是重写规则之前增加约REMOTE_ADDR的previous的RewriteCond(当你没有指定[OR],他们为工作和条件 - 重写规则时,才会触发如果这两个条件都为真 - 不是从该服务器,而不是从PHP脚本)

For the "not from this server" to work, you just add the previous RewriteCond about REMOTE_ADDR before the RewriteRule (when you don't specify [OR], they work as AND conditions - the rewriterule is triggered only if both conditions are true - not from this server and not from the php script)

这篇关于htaccess的:重定向图像一个页面,显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆