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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器