2

Here I am with a question that might have been heard many times but still I could not find a perfect solution for that. My question is that : How to hide the module name , controller name and method name from URL in php codeigniter. Yes I have googled a lot and have seen many of the videos but did not reach to a success.

Below are the lines of code of different files:

<li><a href="<?php echo base_url('index.php/user/UserController/homeIndex');?>">Home</a></li>

In above code there is a ul li menu bar in which when I click on Home it should redirect to the homeIndex method of UserController and user module. UserController is the Controller name homeIndex is the mentho in UserController and user is the module. I am following HMVC structure here.

Currently when I click on home it creates below url http://localhost/KWeds_svn/kweds_hmvc/index.php/user/UserController/homeIndex

Below is my .htacces file code that I am currently using

<IfModule mod_rewrite.c>
  RewriteEngine On
  #RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

Below is the base_url defined in config.php

 $root = "http://".$_SERVER['HTTP_HOST'];
 $root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
 $config['base_url']    = "$root";
$config['index_page'] = 'index.php';

My expected output for the url is only http://localhost/KWeds_svn/kweds_hmvc/

Please let me know how can I hide module name ,controller name and method name.

1

1 Answer 1

3

There is a simple solution to using URI routing in CodeIgniter.

If you open the routes.php file in the config folder you can map URI's to controllers and methods.

In your case, it would work something like this:

$route['KWeds_svn/kweds_hmvc'] = 'UserController/homeIndex';

Also you need to figure out how to remove the index.php from the URI, to do this, use the following steps:

Go to config.php and change

$config['index_page'] = 'index.php';

to

$config['index_page'] = '';

Then go into the document root folder (where the config, application folders are held) and create a new file called .htaccess, in this file write the following code:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

If you are hosting your own server, you may need to enable the rewrite module in apache. To do this, go to the command line for your server and type the following:

sudo a2enmod rewrite

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.