CodeIgniter Forums
Dynamic subdomain routing with .htaccess in CodeIgniter 3 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Dynamic subdomain routing with .htaccess in CodeIgniter 3 (/showthread.php?tid=73239)



Dynamic subdomain routing with .htaccess in CodeIgniter 3 - daniguit - 04-03-2019

Is it possible to route subdomains in a CodeIgniter 3 application using only .htaccess?

What I'm trying to achieve, as an example: I have mysite.localhost.com, and then I'll have multiple subdomains like client1.mysite.localhost.com, client2.mysite.localhost.com, and so on.

I need mysite.localhost.com to point to my 'Home' controller, which will be a landing page.

Then, I need all subdomains to point to mysite.localhost.com/client/, passing the subdomain as a parameter to the 'index' function of the 'client' controller.

I've tried all I could think of, without luck. The following works using plain PHP, but not in CodeIgniter (maybe because of routes.php?), it gives a 500 Internal Server Error (I'm guessing a loop, but I can't figure out how to solve it):

Code:
RewriteCond %{HTTP_HOST} !^mysite\.localhost\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+)\.mysite\.localhost\.com$ [NC]
RewriteRule ^(.*)$ client/%1$1 [L]

To be clear, I need URL masking, not redirecting. Following the above examples, this is what I'm trying to achieve:

Code:
client1.mysite.localhost.com --> mysite.localhost.com/client/client1
client2.mysite.localhost.com --> mysite.localhost.com/client/client2
client3.mysite.localhost.com --> mysite.localhost.com/client/client3

And so on...

Thanks in advance!


RE: Dynamic subdomain routing with .htaccess in CodeIgniter 3 - website - 04-04-2019

You can't add a subdomain in htaccess, look here:

https://stackoverflow.com/questions/19016553/add-subdomain-to-localhost-url