【问题标题】:debugging htaccess rewrite that isn't working调试不起作用的 htaccess 重写
【发布时间】:2013-10-05 17:01:44
【问题描述】:

我有以下 .htaccess 文件,它应该将所有不存在的文件/文件夹重写为 index.php。有人可以建议一种方法来调试为什么这不起作用吗?当我转到一个不存在的文件夹时,我收到一个 404 not-found 页面。

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php

当我访问domain.com/ 时,index.php 文件正确显示

.htaccess 文件与 index.php 位于同一目录中

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    首先,您必须确保mod_rewrite 已启用并正常工作,否则您将不断收到500 Internal Server Error

    您只需使用apache_get_modules() 即可检查。例如,

    // File : test.php
    <?php
    
    if (in_array('mod_rewrite', apache_get_modules()){
       echo 'mod_rewrite is installed and ready to be used';
    } else {
       echo 'mod_rewrite is not installed';
    }
    

    如果它打印出mod_rewrite is installed and ready to be used,那么你可以使用类似的东西,

        RewriteEngine On
    
        # I guess you're missing RewriteBase
        RewriteBase /
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php/$1 [QSA,L]
    

    【讨论】:

      【解决方案2】:

      你能试试这个代码吗:

      RewriteEngine On
      
      RewriteCond %{SCRIPT_FILENAME} !-d
      RewriteCond %{SCRIPT_FILENAME} !-f
      RewriteRule ^ /index.php [L]
      

      【讨论】:

      • 这是放在 DOCUMENT_ROOT 中并启用了 mod_rewrite 和 .htaccess 吗?
      • 我会检查,这是一个全新的服务器,所以可能是罪魁祸首
      • 您是否尝试删除. 所以/index.php
      • @OleksandrKhavdiy:谢谢,是的,这是一个错字。
      • 这真的很奇怪。 .htaccess 是否已启用?你能在这个文件上面放一些垃圾文本,看看它是否会产生 500 错误?
      【解决方案3】:

      你试过了吗

      RewriteEngine On 
      RewriteCond %{REQUEST_FILENAME} !-f 
      RewriteCond %{REQUEST_FILENAME} !-d 
      RewriteRule ^.*$ /index.php [L]
      

      编辑 这是你可能有用的帖子Tips for debugging .htaccess rewrite rules

      【讨论】:

      • 不幸的是,仍然无法正常工作。也许我的问题比 .htaccess 文件的内容大
      • 首先需要确保启用了htaccess
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 2015-02-03
      • 2015-10-28
      • 2014-08-08
      • 2011-07-05
      相关资源
      最近更新 更多