首页 文章

调试htaccess重写不起作用

提问于
浏览
0

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

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

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

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

3 回答

  • 0

    首先,您必须确保 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]
    
  • 1

    你能试试这段代码吗:

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

    你试过这个吗?

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

    编辑这里发布你可能会有用Tips for debugging .htaccess rewrite rules

相关问题