Search code examples
.htaccesscodeigniterlumen

Codeigniter + Lumen Framework - htaccess redirect certain path to specific folder


I have a CodeIgniter project and I recently added a Laravel Lumen API, so the folder structure is the following

Project
 - api/
 - application/
 - public/
 - system/
 - .htaccess
 - index.php

Inside the api/ folder resides the Lumen framework. Locally it's working perfectly, by accessing the url localhost/example.com/api/test it returns 200 HTTP code.

.htaccess codeigniter root folder:

Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [QSA,R,L]
    
    RewriteCond $1 !^(index\.php|public|robots\.txt)
    RewriteCond $1 !^([^\..]+\.php|robots\.txt|sitemap\.xml)

    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>

.htaccess lumen api folder:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

<Files .env>
    Order allow,deny
    Deny from all
</Files>

The errors:

  • By accessing example.com/api/test it returns 404 HTTP code by codeigniter
  • By accessing example.com/api it returns 404 HTTP code by lumen

Solution

  • Solved.

    Well, the .htaccess worked without any problem in localhost because I have apache installed, which I don't have in the production server.

    The server is running Nginx and not Apache, which I only find out after requesting support.