Search code examples
.htaccesscodeigniter

Installing Codeigniter 4 in a subdirectory and keep the domain general URL structure


Current situation:

I already have a Wordpress installation at http://example.com and this site is multilanguage so you can access a web page in English at http://example.com/en/something-here/ and you can access the Spanish version of this webpage at http://example.com/es/algo-aqui/.

What I am trying to do:

I want to install Codeigniter 4 in http://example.com/software/ but I want to keep the general website URL structure, so I need to be able to access the CI4 software through the URL http://example.com/en/software/ and http://example.com/es/software/. Always reading the same CI4 installation mentioned at the beginning of this paragraph.

What I already tried:

I deployed the CI4 files at http://example.com/software/ and modified the .htaccess files of both, the Wordpress site and Code Igniter.

This is my WP .htaccess file on the root directory:

# Disable directory browsing
Options All -Indexes

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

# BEGIN WordPress
# Las directivas (líneas) entre `BEGIN WordPress` y `END WordPress` se generan dinámicamente
# , y solo se deberían modificar mediante filtros de WordPress.
# Cualquier cambio en las directivas que hay entre esos marcadores se sobreescribirán.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

And this is my .htaccess file on the "software" directory:

# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    RewriteBase /

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) /software/index.php/$1

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</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>

# Disable server signature start
    ServerSignature Off
# Disable server signature end

The error I am getting:

I can access both, the site and the software. The site works well but when I try to access let's say http://example.com/en/software/, I get a 404 error when I access Codeigniter. The error message says: "Controller or its method is not found: App\Controllers\En::software".

This is as if CI is trying to find the controller En (that not exist) and the method software.

Any help will be appreciated.


Solution

  • I have solved the problem after trying for 2 days relentlessly. Here's the steps I actually followed-

    First Backup the Codeigniter4 Application by giving the below command as root:

    tar --exclude=".git" -czvf zipped_file_name.tar codeigniter4_project_folder/
    

    upload the codeigniter4_project_folder in public_html directory at Shared Hosing and change the following:

    1. Change the base url in app/Config/App.php or .env file in root folder of codeigniter application (here ci4 is the subdomain which will be configured in cpanel later)

      app.baseURL = 'http://ci4.yourdomain.com/'

    2. Change the Database Config in app/Config/Database.php or .env file in root folder
      database.default.hostname = localhost
      database.default.database = your_db_name
      database.default.username = your_db_user
      database.default.password = your_db_password
      database.default.DBDriver = MySQLi

    3. Change .htaccess file in Public Folder
      RewriteBase /codeigniter4_project_folder/

    4. Add the Subdomain in Cpanel ci4.yourdomain.com which path will be as follows: public_html/codeigniter4_project_folder/public

    Here public folder will be the last in the path. It is very important. Hope this may help others struggling with the problem.