如何通过 HTaccess 在 Codeigniter 中隐藏索引.php和控制器名称



>我想从我的代码 URL 网站隐藏索引.php和控制器名称 我也想替换这个术语 ?SEO =测试产品广告/测试产品

我在下面提到了我的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>

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

这是我的项目文件目录位置http://localhost/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>

通常代码控制器文件演示控制器.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

通常将我们的视图文件编码为演示视图.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>

运行本地主机,仅键入本地主机/project_name/controller_name

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

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

试试这个

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

最新更新