php - 如何通过 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>
解决方案
首先我们在我们的项目目录中添加(.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
如果您的代码未执行,请发表评论
推荐阅读
- javascript - 在后续 PHP 代码中使用 Ajax 分配的变量
- excel - 如何在 OleDB 中选择特定字母的列?
- c - 不添加到结构中
- excel - 给定两个未排序的列范围,如何获得匹配或下限和匹配或更高条目?
- kotlin - 房间:添加一个额外的列
- html - 如何使标题内容响应(小屏幕时在内容 div 顶部堆叠标题图像)
- xml - 使用 XPath 获取特定节点之前的所有内容
- docker - 有什么方法可以更改 kubelet pod 覆盖的默认文件夹?
- python - 如何修复此错误:`HyperlinkedIdentityField` 需要序列化程序上下文中的请求。添加`context={'request': request}`
- angular - 认证后重定向问题