【问题标题】:redirect all files to index.php将所有文件重定向到 index.php
【发布时间】:2014-05-14 08:30:08
【问题描述】:

我尝试将所有文​​件和文件夹重定向到 index.php 以进行 MVC 路由,但它只能从 index.phplearn.delapiata.ro/index.php/i/can/put/anything/here/and/redirect 一样工作

我希望在没有index.php 的情况下工作:learn.delapiata.ro/controllers/any_php_file.php

我在.htaccess使用这个:

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

谢谢。

【问题讨论】:

    标签: php .htaccess mod-rewrite redirect


    【解决方案1】:

    你所拥有的已经可以工作了——但你只需要-f 规则:

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

    这些规则只是意味着:如果请求不指向存在的文件 - 将请求集发送到index.php

    请注意,htaccess 文件只有在 apache 配置中启用时才会生效。例如,如果 htaccess 文件被修改为包含错误(将“asdf”放入其中) - 并且您没有收到 500 响应 - Apache 没有读取 .htaccess 文件,这就是它“不起作用”的原因”。要解决这个问题,请将AllowOverride All 放在您的 apache 配置文件中适当的位置并重新启动 apache。

    【讨论】:

    • 谢谢 AD7six。我已经有了文件规则:RewriteCond %{REQUEST_FILENAME} !-f
    • 我已经尝试过,我在 apache 中有 AllowOverride All 但它不起作用
    • 在答案中阅读我的建议,确定您的 .htaccess 文件是否在做任何事情,然后从那里开始。你得到一个 apache 404 的事实应该表明你没有启用 mod rewrite。
    • 出于兴趣,您的 .htaccess 文件来自哪里?例如,为什么你有一个重写库(通常不需要,也不是“/”),允许文件夹列表和禁用符号链接?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2013-08-26
    • 2016-11-30
    相关资源
    最近更新 更多