-1

I have learned and make website using CodeIgniter. I want to create this system module base so I have created multiple folders and controller,views and model for each module and it's working fine. I want to remove index.php and hide query string like pass user id in URL. I have tried htaccess code which given user guide but it did not work.

Current URL :

mysite/sitename/index.php/departments/departments/delete_department/14 

Require :

mysite/sitename/departments/departments

I have tried below code in htaccess but it does not work as expected, it displays page not found error.

RewriteEngine on
RewriteCond $1 !^(index\.php|images|fonts|js|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

My directory structure is something like that

My site
    |_application
    |   |_ Controller
        |       |_Login (folder) // module folder
        |_Models
        |       |_pages 
        |
        |_views
            |_login (folder) // module folder
halfer
  • 19,824
  • 17
  • 99
  • 186
Hkachhia
  • 4,463
  • 6
  • 41
  • 76

9 Answers9

1

1) you should have Rewrite mod on apache

2) You should change application/config/config.php

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

3) after that I see that you are having a subdirectory of sitename so you should add that on your .htaccess as your RewriteBase which will result in an .htaccess like this:

RewriteEngine On
RewriteBase /sitename/
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
tix3
  • 1,142
  • 8
  • 17
  • I have already enable mode rewrite in apache. How to write subdirectory name in .htaccess file ? i dont know so please help me and give me example code for it. – Hkachhia Jan 12 '15 at 06:49
  • change RewriteBase /sitename/ to RewriteBase /yoursub/ (which I believe is already /sitename/ – tix3 Jan 12 '15 at 13:16
1

If i understand your folder structure right, then your root folder is mysite and your CI application is located at mysite/sitename . if this is the case try checking out the last part of the rewrite

RewriteRule ^(.*)$ /index.php/$1 [L]

As it currently tries to load an index file that is located on your root folder (mysite) and under my understanding your index file is actually located on mysite/sitename, so you can try this:

RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
Lupin
  • 1,225
  • 11
  • 17
1

Using your FTP client, create a new file named .htaccess (including the leading dot) in the same folder as your site’s main index.php file.

Then add the following code to this newly created .htaccess file:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:

RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]

If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:

RewriteRule ^(.*)$ index.php/$1 [L]

If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

To:

RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]

And change:

RewriteRule ^(.*)$ /index.php/$1 [L]

To:

RewriteRule ^(.*)$ testing/index.php/$1 [L]

If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:

RewriteRule ^(.*)$ /index.php?/$1 [L]
0

do you have apache rewrite-mod enable ?

you must have apache rewrite-mod enabled to do that .. Check your config at apache configuration path ..

0

change $config['index_page'] = 'index.php' to $config['index_page'] = '' from

application\config\config.php

Also check your apache config for mod rewrite is enabled or not.

Checking URl Rewrite is Working or Not

MOD Rewrite

$isTrue = in_array('mod_rewrite', apache_get_modules());

Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.

You can test it using the following, though

$isTrue = strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false

If the above evaulates to true, then mod_write is enabled.

Hoja
  • 1,057
  • 1
  • 12
  • 32
0

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]
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
0

You are using the rewrite engine of apache. Make sure mod_rewrite is enabled and make sure your regular expression fulfills your needs.

Read about mod_rewrite here.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

The htaccess rules look OK. Because you havent shown your code its hard to say but its either one of the following:

  • Your having an issue with your controller and method both being called 'departments'. If this is true either try renaming the method to something other than 'departments' or getting rid of it all together.

  • You dont have a controller called 'departments' (it doesnt show in your directory structure) or a route to handle the URI 'departments'. Either create the controller or add a route to direct the URI 'departments' to the controller you want to use. ie:

    $route['departments/departments']='an_existing_controller/an_existing_method';

  • You dont have a method called 'departments'. If this is true get rid of the additional URI component called 'departments' or create a new method (as above dont call your method the same thing as your controller).

Mike Miller
  • 3,071
  • 3
  • 25
  • 32
0

In config.php (which is mostly loated in application/config/config.php make index page value to empty string, like this,

$config['index_page'] = '';

In root folder of your application make a .htaccess file with following code,

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Afzal Ahmad
  • 586
  • 5
  • 20