dpg98445 2012-06-30 15:29
浏览 20
已采纳

Mod_Rewrite中的这两行有什么作用?

I've benn trying to figure out what these two lines in Mod_Rewrite do and would appreciate some help. Thanks in advance.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
  • 写回答

1条回答 默认 最新

  • dq804806 2012-06-30 15:31
    关注

    They check if the requested URI is an existing file or a directory. The ! in front makes the condition "not", thus,, the request does NOT map to a file or directory.

    See the -f and -d description in mod_rewrite


    Don't all requests map to some file so that it can be loaded by the browser?

    No. The request could be for something that doesn't exist and be rewritten. For example, http://en.wikipedia.org/wiki/something would mean the URI is /wiki/something, which doesn't map to any physical file or directory. But internally, there is a rule that rewrites /wiki/something to index.php?title=something, and index.php does exist.


    Edit: for edited question

    • The 2 conditions: if the requested URI doesn't map to a file or a directory, apply the following rule.
    • The rule: Take whatever the request is, and append a .php to the end.
    • The logic behind this: mod_rewrite loops until the URI that goes into the rewrite engine and the URI that comes out of it is identical. Without the check to see if the URI maps to a physical file or directory, the rule will loop:
      • URI in = /something
      • Check conditions: /something doesn't exist
      • Apply rule, URI in = /something.php
      • Check conditions: /something.php EXISTS, don't apply rule
      • URI in = URI out = /something.php, stop rewriting
    • Otherwise, without the !-f and !-d checks:
      • URI in = /something
      • Apply rule, URI = /something.php
      • URI in = /something.php
      • Apply rule, URI = /something.php.php
      • URI in = /something.php.php
      • Apply rule, URI = /something.php.php.php
      • loop indefinitely until apache decides to stop and return a 500 error
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式