简体   繁体   中英

Codeigniter htaccess - controllers in subfolder not working

I am new to CI and don't know much about rewrite rules. I was able to find htaccess rules from stackoverflow post which allows me to remove index.php from the URL. The ht access rules are as following

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /siebeluigen/

 #Removes access to the system folder by users.
 #Additionally this will allow you to create a System.php controller,
 #previously this would not have been possible.
 #'system' can be replaced if you have renamed your system folder.
 RewriteCond %{REQUEST_URI} ^system.*
 RewriteRule ^(.*)$ /index.php?/$1 [L]

 #When your application folder isn't in the system folder
 #This snippet prevents user access to the application folder
 #Submitted by: Fabdrol
 #Rename 'application' to your applications folder name.
 RewriteCond %{REQUEST_URI} ^application.* 
 RewriteRule ^(.*)$ /index.php?/$1 [L]

 #Checks to see if the user is attempting to access a valid file,
 #such as an image or css document, if this isn't true it sends the
 #request to index.php
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
 # If we don't have mod_rewrite installed, all 404's
 # can be sent to index.php, and everything works as normal.
 # Submitted by: ElliotHaughin

 ErrorDocument 404 /index.php
</IfModule>

But this doesn't work if I move my controllers to subfolder within the controller directly. for example

If I put my Application controller in following path

application/controllers/application.php

Then I can access it from URL

http://localhost/siebeluigen/application

But if my directory structure is

application/controllers/app/application.php

Then I get error with following url

http://localhost/siebeluigen/app/application

but it works like this

http://localhost/siebeluigen/index.php/app/application

So, I am pretty sure there is something in htaccess that I should be able to change to make it work.

Please help!!!

Update

My htaccess file was in the application folder instead of root. Moving the file to the folder were index.php is located solved the issue. I have marked the answer that is working. Thanks...

This is my htaccess file that I use on ALL my projects:

Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

    # activate URL rewriting
    RewriteEngine on

    # do not rewrite links to the documentation, assets and public files
    RewriteCond $1 !^(images|assets|uploads|captcha)

    # do not rewrite for php files in the document root, robots.txt or the maintenance page
    RewriteCond $1 !^([^\..]+\.php|robots\.txt)

    # but rewrite everything else
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>

    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    ErrorDocument 404 index.php

</IfModule>  

Then - also make sure that inside your config.php file

  $config['index_page'] = '';

edit: you NEVER want to point someone to your application folder - you ALWAYS point them to your index.php - EVERYTHING goes through Index.php - and it does the re-routing to where it is needed.

This is the condition that is usually meant to redirect to application.php

RewriteCond %{REQUEST_URI} ^application.* 

app/application doesn't match this condition. Try changing it to this:

RewriteCond %{REQUEST_URI} ^app/application.* 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM