Subdomains Substitutions Substitutions with CodeIgniter

I tried to circle my head around this concept and how to properly direct it, but it's just hard for me to do it.

I accept all domains and subdomains for my application, but here is the problem:

Example:

john.myapp.com/foo 

it should go to the foo function in the user controller instead of calling the controller named foo .

So:

 john.myapp.com/foo -> myapp.com/user/foo john.myapp.com/foo/bar -> myapp.com/user/foo/bar someotherdomain.com/foo/bar -> myapp.com/user/foo/bar myapp.com/foo -> the controller: foo 

But this routing should only apply to subdomains and other domains, not to myapp.com

+4
source share
1 answer

Codeigniter does not support domain routing as part of its default functionality.

If you visited www.example.com/controller/method , the router only interprets the /controller/method and ignores nothing before.

This means that in order to achieve what you are trying, you need to direct subdomains to the application using .htaccess.

 RewriteCond %{HTTP_HOST} ^((?!www\.)(?!myapp\.com)[^\.]+)\. RewriteRule ^(.*)$ /index.php?/user/%1/$1 [L] 

This will send all requests e.g.

subdomain.example.com/controller/method go to example.com/index.php/user/controller/method

This will allow you to route based on the subdomain using standard routing functionality.

+6
source

Source: https://habr.com/ru/post/1390826/


All Articles