【问题标题】:how to hide index.php and controller name in codeigniter by htaccess如何通过 htaccess 在 codeigniter 中隐藏 index.php 和控制器名称
【发布时间】:2019-09-05 07:37:38
【问题描述】:

我想从我的 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 .htaccess codeigniter


【解决方案1】:

首先我们在我们的项目目录中添加(.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

如果你的代码没有执行,请评论

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多