Search code examples
phpcodeigniterhttp-redirectconfigurationcodeigniter-4

CodeIgniter 4 site_url returns the old domain


I made a web application with CodeIgniter 4 and moved it to another domain and also to another server. After I configured the database I went to test it and it always redirects me back to the old domain.

I checked what the site_url() function returns and it returns the old website.. Then I did an extensive search for all the files in my project to find where I might have a hard-coded old domain but nothing came up..

how can I fix this and where else can I change the base url?

It is probably not important but the application was previously on subdomain but now it is on main domain (test.old-domain.com vs domain.com)


Solution

  • If your new CodeIgniter 4 site is redirecting to an old domain, you need to check mainly 3 files:

    • project-root/app/Config/App.php: check your baseUrl() setting: around line 19, set to the new domain: public $baseURL = 'https://myNewDomaindotcom';:

      Note

      Since v4.3.0, if you set Config\App::$allowedHostnames, this returns the URL with the hostname set in it if the current URL matches.

    • project-root/public/.htaccess: check if you have any RewriteRule redirecting to the old domain

    • project-root/.env: around line 25 you can enable: app.baseURL = 'myNewDomaindotcom'

    more about Initial Configuration


    concerning OP's comment:

    yes, it was in .env file and the search was unable to find it for some reason

    depending on your favorite IDE, you might need to associate .env files with your IDE in order to have the .env file contents searched (or just search it manually)