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.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法