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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?