У меня проблема с маршрутизации в CodeIgniter, я искал и пробовал другие ссылки в stackoverflow, включая Removing index.php в CodeIgniter, просто работает для URL-адреса контроллера по умолчанию, но моя проблема не решена. Когда я запускаю из этого URL-адреса http://localhost/MyProject/, он успешно выводит страницу просмотра страницы test.php, но проблема в том, что есть также просмотр и контроллер test2, поэтому я не могу загрузить его напрямую этим url http://localhost/MyProject/test2, но я могу загрузить его через http://localhost/MyProject/index.php/test2. Я хочу, чтобы я мог запускать свой код без index.php между URL-адресом, например http://localhost/MyProject/test2.
View
test.php
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>Welcome Test </title>
</head>
<body>
<div>
<span>Test 1</span>
</div>
</body>
</html>
Controller
test.php
<?php
class test extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('test');
}
}
?>
routes.php
$route['default_controller'] = "test";
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
#rename "MyProject/" with your application directory
#For example you rename the entire CodeIgniter application as "mysite"
#then, the "RewriteBase /" will be like this "RewriteBase /mysite"
#same as at line number 10, "RewriteRule ^(.*)$ /MyProject//index.php #/$1 [L]" will be like this "RewriteRule ^(.*)$ /mysite/index.php/$1 [L]"
RewriteBase /MyProject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /MyProject/index.php/$1 [L]
</IfModule>
Вам нужно удалить index.php из файла url.Create.htacces и поместить его в свой root.Copy этот код и запустить
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
подробнее о том, как удалить index.php form url, попробуйте это http://w3code.in/2015/09/how-to-remove-index-php-file-from-codeigniter-url/
FYI
Рекомендуемый .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]