CodeIgniter - redirects subdomains installed to a folder in "controllers"

I am trying to redirect any or a set of subdomains to a folder inside the "controllers" folder of the CI installation. I tried a bunch of things found here on SO, but nobody works for my project or does not have the same specifications that I need. Since I'm a little noob when it comes to .htaccess, so I decided that I could just ask someone more qualified here. Here are the specifications:

  • using this amazing .htaccess file as a base
  • you must redirect at least these three subdomains (www | admin | api) to / application / controllers / (www | admin | api) equivalent folders
  • without losing REQUEST_URI (just saying)
  • and without changing the URL in the address bar

Example: http: // api .domain.com / some / uri / segments should be internally redirected to CI_installation_folder / application / controller / api / some // URI segments

I tried something like this (and options):

RewriteCond %{HTTP_HOST} ^(www|admin|api) [NC] RewriteRule ^(.*)$ /%1/$1 [L,R=301] 

or replaced RewriteRule with two other lines:

 RewriteCond %{ENV:REDIRECTED} !true RewriteRule ^(.*)$ [L,R=301,E=REDIRECTED:true] 

to prevent a loop, but all I can get is either a cyclic redirection (1st case), or even a 500-server error in some cases: (

Adding this

 RewriteCond %{REQUEST_URI} !^/(www|admin|api) [NC] 

will also not work, since I am not changing the URL in the address bar. I also did not succeed with the flag [P].

Can anyone help? Thanks!

+6
source share
2 answers

Have you tried using the Codeigniter route configuration?

you do not need to use htaccess rewrite - although its a valid approach, you can just check the subdomain in the config/route.php and set the routing for your subdomains.

 switch ($_SERVER['HTTP_HOST']) { case 'admin.domain.com': $route['(:any)'] = "admin/$1"; // this will set any uri and add the controler fodler to it $route['default_controller'] = "admin/home"; // set the default controller for this subdomain break; case 'api.domain.com': $route['(:any)'] = "api/$1"; // this will set any uri and add the controler fodler to it $route['default_controller'] = "api/home"; // set the default controller for this subdomain break; } 

If you want this to be more general / dynamic routing, you can get it like this (in the same config/route.php ):

 $controllerFolderName = array_shift((explode(".",$_SERVER['HTTP_HOST']))); $route['(:any)'] = $controllerFolderName."/$1"; $route['default_controller'] = $controllerFolderName."/home"; 

this routing will work for all subdomains and will set the default routing to a folder inside the controller folder with the same name as the subdomain, so for a domain such as api.domain.com, you will have a route set to api, etc. ..

it is important that you keep the same logic for all folder names that will always correspond to your subdomain, and I also suggest adding an error handling system for visitors without a subdomain ( http://domain.com ), and for cases when you have a subdomain, but a folder with this name does not exist (you can do this with file_exits )

+4
source

After several hours of excavation, I think I solved the problem. Here's how (for those who care):

# Subdomains to Folders + Enforce www RewriteCond %{HTTP_HOST} ^(www|admin|api) [NC] RewriteRule ^(.*)$ http://www.localhost/%1/$1 [L,P,S=1] RewriteRule ^(.*)$ http://www.localhost/$1 [L,R=301]

I combined the internal redirect with the www enforcer rule. All you need to do after this is configure Apache Server to accept and redirect the PROXY request correctly :)

You have a phun!

+1
source

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


All Articles