首页 文章

在codeigniter中使用备用路由删除index.php

提问于
浏览
0

我正在使用Codeigniter中的一个项目,该项目不使用标准的routing.php文件 . 它调用这样的函数:test.vi/index.php/controller/function .

我的目标是通过htaccess重写从url中删除index.php:

RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule .* index.php/$0 [PT,L]

而且这个config.php改变了:

$config['index_page'] = '';

我的问题是我需要在route.php中路由所有控制器/函数,还是可以在不使用route.php文件的情况下完成?

4 回答

  • 0

    首先确保你在httpd.conf中启用了mod_rewrite:

    这样做添加/确保它存在这一行

    LoadModule rewrite_module modules/mod_rewrite.so
    

    然后在codeigniter根文件夹(index.php文件旁边)中创建.htaccess而不是应用程序文件夹 .

    的.htaccess

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

    codeigniter为您处理所有路线;因此对于

    controlers/welcome.php method=hello 你可以通过它访问它

    http://localhost/welcome/hello

    它不像laravel你需要专门添加路线; codeigniter自动为您的惠誉控制器 . 除非你将它存储在不同的位置或子文件夹中,否则你不需要触摸route.php,除了设置你的默认路由 .

  • 0

    这是我在.htaccess中重定向的实时codeigniter网站 . 第二部分免除了某些目录 .

    #Redirect non-www to www:
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    RewriteCond $1     !^(index\.php|phpinfo\.php|test\.php|example\.php|images|i|css|js|beta|captcha|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
  • 0

    这对我来说很完美......试一试..

    RewriteEngine on
     RewriteCond %{REQUEST_FILENAME} !-s
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ index.php?/$1 [L]
    
  • 3

    试试这个

    RewriteEngine on
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ /test.v/index.php?/$1 [L]
    

    我想每个人都忘了在.htaccess上添加test.vi文件夹名称

相关问题