how to hide index.php and controller name in codeigniter by htaccess

ぃ、小莉子 提交于 2021-02-05 12:22:09

问题


i want to hide index.php and controller name from my codeignier url website also i want to replace this term ?seo=test-product ad /test-product

i have mention my htaccess file below please guild me how to fix this issue i have tried many things none helps

 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>

回答1:


first we adding (.htaccess)(it is only extention file) file extention in our project directory

it is my project file directory location http://localhost/demoproject demoproject is my project name

copy below code and create (.htaccess) file and paste it under (.htaccess) file
these file create under project directory

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


# Disable Directory Browsing
Options All -Indexes

one more (.htaccess) file create into view folder

copy below code and paste it under newly created (.htaccess) file

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

normally code controller file 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

normally code our view file 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>

run localhost and only type localhost/project_name/controller_name

here we use demoproject as project name and DemoController as controller name http://localhost/demoproject/DemoController

if your code not execute then please comment




回答2:


try this

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


来源:https://stackoverflow.com/questions/57800579/how-to-hide-index-php-and-controller-name-in-codeigniter-by-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!