【问题标题】:How to Remove index.php from URL in PHP如何在 PHP 中从 URL 中删除 index.php
【发布时间】:2011-10-28 19:52:47
【问题描述】:

我想从 url 中删除 index.php,但到目前为止我无法成功。我在本地服务器上使用 Wamp 服务器,在远程服务器上使用 Apache。 在本地根目录中,我的项目文件位于像

这样的子文件夹中
www/project/index.php

我可以访问像

这样的网页

localhost/project/index.php/home

localhost/project/index.php/messages/?mId=3

我只想像访问它一样

localhost/project/home
localhost/project/messages/?mId=3

我已经尝试了一些 .htaccess 重写规则,但无法做到。

【问题讨论】:

    标签: .htaccess url-rewriting


    【解决方案1】:

    以下是您需要的组织方式:

    <ifModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
    </ifModule>
    

    然后您将拥有所需的一切。

    【讨论】:

      【解决方案2】:

      您将希望通过 .htaccess 文件使用 url 重写:

      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
      </IfModule>
      

      在每个子文件夹中放置一个包含此内容的 .htaccess。

      【讨论】:

      • 感谢 Martin,它正在工作.. 但还有一个问题,我仍然可以使用 index.php 输入 url 而不会删除 index.php。当我输入 localhost/project/home 时它正在工作,但是当我输入 /localhost/project/index.php/home 时它仍然在那里?
      • 在 .htaccess 更改后 $_SERVER['PATH_INFO'] 也只是空的。我在 index.php 中使用它来计算来自 url 的控制器名称。
      • 是的,有了这个重写条件,所有东西都可以在 $_GET['url'] 而不是 PATH_INFO 中访问
      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2013-05-15
      • 2019-08-16
      • 2012-08-10
      相关资源
      最近更新 更多