简体   繁体   English

使用.htaccess从URL中删除.pdf文件扩展名

[英]Remove .pdf file extension from URL using .htaccess

I have a PDF file on my server and I want the extension to be removed when accessed thorough the browser. 我的服务器上有一个PDF文件,我希望通过浏览器访问该扩展名时将其删除。

I have added a redirect on .htaccess like: 我在.htaccess上添加了重定向,例如:

Rediect permenent /somedestination http://example.com/somefile.pdf

I have tried many of redirect rules but all of them is for hiding the .html, .php etc. extensions but I'm not able to remove the PDF extension. 我已经尝试了许多重定向规则,但是所有这些规则都是用于隐藏.html,.php等扩展名的,但我无法删除PDF扩展名。

I'm new to this .htaccess scripting so any help would be greatly appreciated 我是这个.htaccess脚本的新手,所以将不胜感激任何帮助

The rewrite rules that "remove" the html, php, or any other extension will look the same except you simply replace the "html" or whatever with "pdf". “删除” html,php或任何其他扩展名的重写规则将看起来相同,只是您只需将“ html”或任何其他内容替换为“ pdf”即可。

For example, look at this question: remove .html extension from url 例如,看这个问题: 从url删除.html扩展名

Follow those steps, and take those rules and replace "html" with "pdf": 请遵循这些步骤,并遵循这些规则,并将“ html”替换为“ pdf”:

## hide .pdf extension
# To externally redirect /dir/file.pdf to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.pdf[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]

# To internally forward /dir/file to /dir/file.pdf
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.pdf -f [NC]
RewriteRule ^(.+?)/?$ /$1.pdf [L]

Someone feel free to correct me, but I don't think its actually possible. 有人可以纠正我,但我认为这实际上是不可能的。 Opening a PDF in browser is much the same as serving an image. 在浏览器中打开PDF与提供图片几乎相同。 Your browser is directly opening and interpreting the file. 您的浏览器正在直接打开并解释文件。

That being said, I can think of exceptions. 话虽如此,我可以想到例外。 Imgur can serve animated .gif images but the URL will read .jpg, so there may be some way to mask it still. Imgur可以提供动画的.gif图像,但URL会显示.jpg,因此可能有一些方法可以将其屏蔽。

A possible alternative would be to find something to display the PDF on a separate page that you generate a name for. 一种可能的替代方法是找到一些在您为其生成名称的单独页面上显示PDF的内容。 A 'hack-y' method might be to try and use an iframe? 一种“ hack-y”方法可能是尝试使用iframe? But there may be js plugins you could use or something similar. 但是可能有您可以使用的js插件或类似的插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM