drm30963 2019-07-13 01:42
浏览 126
已采纳

通过htaccess将扩展名插入到没有扩展名的文件中

I have several files without extension for example:

NAME 1
NAME 2

They are not extension, bit I would like to insert extension .PDF using htaccess.

When accessing the url

localhost / NAME1

even without extension through htaccess recognize .pdf extension

  • 写回答

1条回答 默认 最新

  • dongmin4990 2019-07-13 11:08
    关注

    I understand the question such that your actual files in the server side file system do have file name extensions ("/path/to/xxx.pdf"), but that this extension is missing in the requested URLs. If that assumption is corrrect (your question is vage...), then this probably is what you are looking for:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.pdf -f
    RewriteRule ^/?(.+)$ /$1.pdf [END]
    

    Here it is important that you do not simply copy and paste that snippet, but that you actually understand what is coded there, so that you can adapt it to your specific needs. The excellent documentation of the rewriting module is a good help in that: https://httpd.apache.org/docs/current/mod/mod_rewrite.html

    In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

    This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

    And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

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

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用