duanchi8836 2014-12-09 22:02
浏览 61
已采纳

使用.htaccess伪装.pdf扩展名,避免循环

I have a little bit stupid necessity to disguise links to PDF files as PHP files with the PDF filename as a parameter.

Something like:

  1. Client requests xxxx.pdf file.
  2. Server receives request but instead of returning xxxx.pdf goes to yyyy.php or whatever extension.
  3. yyyy.php has an iframe in which the PDF file is loaded.

The thing is that I managed to do point 2. but when I try to do point 3. it either cycles or doesn't locate the file.

I was thinking of having a custom extension to "simulate" another file in order to not cycle but it's still cycling.

I have the following .htaccess:

AddType application/pdf         .view
RewriteRule ^(.*)\.pdf$         yyyy.php?filename=$1 [L]
RewriteRule ^(.*)\.view$        $1.pdf [L]

The thing is that it cycles and I get nowhere.

I am not really good with .htaccess, so where am I doing this wrong?

  • 写回答

2条回答 默认 最新

  • doujian7132 2014-12-10 16:36
    关注

    I found it easier to just redirect when I detect a .pdf extension.

    So I basically did this:

    1. .htaccess detects a PDF extension and redirects to a php file passing the filename without extension as a parameter.
    2. The php file uses the filename and renders the PDF using header and readfile to avoid conflicts with .htaccess.

    Something like that:

    .htaccess

    RewriteRule     ^(.*)\.pdf$     /pdfreader/$1 [L,R=302,NC]
    RewriteRule     ^pdfreader/(.*)?$           pdfreader.php?file=$1 [L,NC]
    

    pdfreader.php

    $file = $_GET["file"].".pdf";
    
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $file . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($file));
    header('Accept-Ranges: bytes');
    
    @readfile($file);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题