首页 > 解决方案 > 如何通过 htaccess 在 codeigniter 中隐藏 index.php 和控制器名称

问题描述

我想从我的 codeignier url 网站中隐藏 index.php 和控制器名称我也想替换这个词 ?seo=test-product ad /test-product

我在下面提到了我的 htaccess 文件请指导我如何解决这个问题我已经尝试了很多事情都没有帮助

 RewriteEngine On
 RewriteCond %{HTTPS} off [OR]
 RewriteCond %{HTTP_HOST} ^www\. [NC]
 RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
 RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>

标签: php.htaccesscodeigniter

解决方案


首先我们在我们的项目目录中添加(.htaccess)(它只是扩展文件)文件扩展

它是我的项目文件目录位置 http://localhost/demoproject demoproject 是我的项目名称

复制下面的代码并创建(.htaccess)文件并将其粘贴到(.htaccess)文件
下,这些文件在项目目录下创建

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]


# Disable Directory Browsing
Options All -Indexes

另一个(.htaccess)文件创建到视图文件夹中

复制下面的代码并将其粘贴到新创建的(.htaccess)文件下

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

通常代码控制器文件 DemoController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class DemoController extends CI_Controller 
{
	public function index()
	{
        $data['demo'] = 'hello world';
        $this->load->view('DemoView', $data);
	}
}	

enter code here

通常对我们的视图文件 DemoView.php进行编码

<!DOCTYPE html>
<html>
<head>
	<title>demo</title>
</head>
<body>
      <h1>
      	<!-- $demo is the $data of object that defind our DemoController. -->
           <?php echo $demo ?>
      </h1>
</body>
</html>

运行 localhost 并且只输入 localhost/project_name/controller_name

这里我们使用 demoproject 作为项目名称和 DemoController 作为控制器名称 http://localhost/demoproject/DemoController

如果您的代码未执行,请发表评论


推荐阅读