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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!