doushi1929 2010-09-25 17:37
浏览 5
已采纳

文件不存在时出现内部错误

I want get everything up to the "/" and merge with ".php"

works fine, but when the file does not exist I get internal error

RewriteRule ([^\/]*) $1.php
  • 写回答

2条回答 默认 最新

  • dongtao9887 2010-09-26 04:25
    关注

    You might want to check that what you're rewriting to exists before performing the rewrite, to avoid a loop (noting that your test pattern matches what you rewrite to). One possible approach to this is to do the following:

    # Check that the file isn't a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # ...nor is it a file
    RewriteCond %{REQUEST_FILENAME} !-f
    # ...but the result of the rewrite will be
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^/]+) $1.php
    

    This also has the (minor) benefit of showing the original URL in cases where a 404 is generated, provided you're using the stock Apache error message. If you were to rewrite to a non-existent PHP file, the server would say that /something.php could not be found, instead of /something/, but since the rewrite is not performed when the target file does not exist, you get /something/ instead.

    If you still get a 500 error, there's likely another conflicting rule in your .htaccess file. You should also check Apache's error log for more detailed information about what the error was, although in this case I suspect that it was an internal redirection limit exceeded message.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序