Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter 404 on valid routes when using custom htaccess mod rewrites

Im having an issue with codeignighter when using custom (somewhat strange) htaccess folder mapping. However, it should technically work, but it does not. Can someone help me?


Scenario 1 (Works Fine):

Folder structure and key files

/ is the website root
/.htaccess
/api/ <this is the CodeIgniter Root, with index.php and default CI .htaccess>

/.htaccess:

RewriteEngine On
RewriteRule ^api/(.*)$ /api/$1 [L]

accessing mydomain.com/api/admin/ for example gets me to my admin-controller/index action and loads the views fine.

var_dump on index.php for $_REQUEST shows

array (size=1)
'admin/' => string '' (length=0)

Scenario 2 (Does not work):

Folder structure and key files

/.htaccess
/current/.htaccess
/current/api/ <this is the CodeIgniter Root, with index.php and default CI .htaccess, this is the same folder/files/htaccess as above scenario>

/.htaccess:

RewriteEngine On
RewriteRule ^(.*)$ /current/$1 [L]

/current/.htaccess:

RewriteEngine On
RewriteRule ^api/(.*)$ /api/$1 [L]

accessing mydomain.com/api/admin/ for example gets me to the Codeigniter 404 Page/view.

var_dump on index.php for $_REQUEST shows (Same as Scenario 1)

array (size=1)
'admin/' => string '' (length=0)

What is going on here? I know the htaccess rewrites are strange, but as far as what Codeigniter sees, the route is the same (admin/). But they both behave differently. Why? I tried everything i could think of, as far as i can see, codeigniter sees the right path as far as the Request Object goes, but the CI router somehow does not use it correctly.

Any help appreciated, let me know if you can think of anything i can try to help debug this further.

Note: the reason im using double htaccess is so that i can schedule different instances of CI depending on time/scenario (you can use rewrite condition to serve different folders instead of the "current" folder). Im currently not using this, so in scenario 2, the root only has the one "current" folder.

like image 555
decay Avatar asked Aug 11 '15 04:08

decay


1 Answers

Removing index.php from the URI at CodeIgniter can be a pain. Last week I was confronted to a similar case: I have my root web directory at /var/www/html and inside /ci my project... so inside /ci I have the following .htaccess:

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

and in the apache2 config file I added the following:

<Directory /var/www/html/ci/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Also check that in application/config/config.php you have:

$config['uri_protocol'] = 'REQUEST_URI';

Hope that helps...

like image 65
raziel Avatar answered Nov 13 '22 03:11

raziel