【问题标题】:How can I remove index.php from URL? [duplicate]如何从 URL 中删除 index.php? [复制]
【发布时间】:2011-11-10 08:07:32
【问题描述】:

可能重复:
CodeIgniter - How to hide index.php from the URL

如何使用 .htaccess 从 URL 中删除 index.php,例如当用户输入时:

http://localhost/index.php/welcome

它会变成:

http://localhost/welcome

目前 使用我当前的.htaccess,上面的两个 url 都可以访问控制器welcome,但我只想要控制器的 1 个有效 url。

RewriteEngine on
RewriteCond $1 !^(index\.php|admin_assets|img|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    简单快速的解决方案:

    只需使用以下代码创建一个.htaccess 文件

    重写引擎开启 RewriteCond $1 !^(admin_assets|img|css|js|robots\.txt|favicon\.ico) 重写规则 ^(.*)$ index.php/$1 [L]

    然后将它放在index.php 附近的CI 应用程序的根目录下。
    这个修改后的.htaccess 文件满足您的要求,(即you don't want the user to be able to access the same page with 2 different urls's

    问。它会做什么?

    A. 它会将所有对您的应用程序的请求引导到 index.php,包括那些包含 index.php 的 url,除了(admin_assets,img,css,js 文件夹和您的 robots.txt,favicon.ico 文件)显然在 apache 上。

    CodeIgniter 自己的文档很好,URL's in CI

    【讨论】:

      【解决方案2】:

      在你的 config.php 文件中搜索这一行:

      $config['index_page'] = "index.php";
      

      像这样编辑它:

      $config['index_page'] = ""; //remove index.php from there
      

      并将其添加到您的 htaccess 中:

      Options +FollowSymLinks
      
      Options -Indexes
      
      DirectoryIndex index.php
      
      RewriteEngine on
      
      
      
      RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico|sitemap\.xml)
      
      RewriteCond %{REQUEST_FILENAME} !-f
      
      RewriteCond %{REQUEST_FILENAME} !-d
      
      RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
      

      希望这会有所帮助。

      【讨论】:

      • 这也行不通,Zekiel 想重定向:http://localhost/index.php/welcomehttp://localhost/welcome 他说 url 重写作品他只需要重定向
      【解决方案3】:

      大约有 50000 个问题处理这个问题,还有一篇关于它的大型 wiki 帖子

      http://codeigniter.com/wiki/mod_rewrite/

      ModRewrite not working for codeigniter site

      Rewrite URLs in CodeIgniter

      CodeIgniter - How to hide index.php from the URL

      ...这些应该有帮助

      【讨论】:

      • 不,这些并没有告诉我当用户输入 index.php/welcome 时如何从 URL 中实际删除 index.php。基本上,所有文章和主题都是关于使localhost/welcome 成为有效请求。
      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2013-05-15
      • 2019-08-16
      • 2012-08-10
      相关资源
      最近更新 更多