dqyat62284 2013-02-11 17:02
浏览 197
已采纳

重定向直接图像URL请求并在重定向中传递原始URL值

Suppose I have someone (or Google Images) trying to access

http://null.com/uploads/someimage.jpg (actually any image in /uploads folder)

I'd want htaccess to redirect the user to

http://null.com/page/

HOWEVER, I need to pass the original url (http://null.com/uploads/someimage.jpg) to the redirection target page as a POST string value which the target php page can use to do stuff with it

Would it be possible to achieve that with htaccess?

UPDATE - and also, it would be interesting to have this working only when the user agent is a human operated browser, not a bot such as google bot, in this way Google and other search engines can crawl images appropriately without the redirection

  • 写回答

1条回答 默认 最新

  • doutui6644 2013-02-11 18:31
    关注

    You may try this:

    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !page\.php           [NC]
    RewriteCond %{REQUEST_URI} ^/uploads/([^/]+)/?  [NC]
    RewriteRule .  page.php?url=uploads/%1 [L]
    

    Maps silently

    http://null.com/uploads/someimage.jpg with or without trailing slash

    To:

    http://null.com/page.php?url=uploads/someimage.jpg

    The string uploads is assumed to be fixed, while someimage.jpg can be any name.

    The script name page.php is an example an can be any name also. Replace all instances in the rule-set.

    For permanent and visible redirection, replace [L] with [R=301,L]

    This answer is according to this description in OP comments:

    "...redirect the user to this page and pass the former original url as a post value..."

    where page is a script.

    NOTE: The parameters passed in the substitution URL can be captured with PHP using $_GET or $_REQUEST, not $_POST.

    UPDATE

    Here is another version:

    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !page\.php        [NC]
    RewriteRule ^(.*)\.jpg   page.php?url=$1.jpg [L]
    

    Maps silently

    http://null.com/any/number/of/folders/someimage.jpg

    To:

    http://null.com/page.php?url=any/number/of/folders/someimage.jpg

    The rewrite rule is applied only when the incoming URL holds a file with extension jpg at the end of the path.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合