如何从URL中删除index.php?

时间:2011-09-05 02:58:48

标签: php .htaccess codeigniter

  

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

如何使用index.php从网址中删除.htaccess,以便用户输入时,例如:

  

http://localhost/index.php/welcome

它将成为:

  

http://localhost/welcome

目前 使用我当前的.htaccess,上面的两个网址都可以访问控制器welcome,但我只想为控制器提供一个有效的网址。

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

3 个答案:

答案 0 :(得分:1)

在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]

希望这有帮助。

答案 1 :(得分:0)

答案 2 :(得分:0)

简单快捷的解决方案:

  

只需使用以下代码

创建.htaccess文件即可
RewriteEngine on
RewriteCond $1 !^(admin_assets|img|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

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

  

Q值。它会做什么?

  答:它会指导您的应用程序的所有请求到index.php,包括那些包含index.php的网址,除了(admin_assets,img,css,js文件夹和你的robots.txt,favicon.ico文件)显然在apache上。< / p>

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

相关问题