Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter - redirect subdomains set to folder in "controllers"

I'm trying to make any or a set of subdomains redirect to a folder inside "controllers" folder of a CI installation. I've tried a bunch of stuff found here on SO but none work for my project or have the same specs I need. Since I am a bit of a noob when it comes to .htaccess so I've figured I might just ask someone more qualified in here. Here are the specs:

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

Example: http://api.domain.com/some/uri/segments should internally redirect to CI_installation_folder/application/controllers/api/some/uri/segments

I've tried something like this (and variations):

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

or replaced the RewriteRule with 2 other lines like so:

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

to prevent the loop, but all I can get is either a looping redirect (1st case) or even a 500 server error on some variations :(

Adding this

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

will also not work since I'm not changing the URL in the address bar. I also haven't got any success with the [P] flag also.

Can anyone help? Thanks!

like image 584
Mihai Mihalache Avatar asked Jan 25 '15 18:01

Mihai Mihalache


Video Answer


2 Answers

Did you try using the route configuration of Codeigniter?

you don't have to use htaccess rewrite - although its a valid approach, you can just check for the subdomain in the config/route.php file 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 would like it to be a more generic/ dynamic routing, you can have it like this (in the same config/route.php file):

$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 like api.domain.com you will have the route set to api etc.

it is important that you keep the same logic for all folder name that they will always match your subdomain and i would also suggest to add an error handling system for visitors with no subdomain (http://domain.com) and for cases where you have the subdomain but a folder with that name does not exists (you can do that with file_exits)

like image 158
Lupin Avatar answered Oct 06 '22 14:10

Lupin


After a few more hours of digging I think I solved the problem. Here's how (for the ones that 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 there is to do after this is configure the Apache Server to accept and properly redirect the PROXY request :)

Have phun!

like image 42
Mihai Mihalache Avatar answered Oct 06 '22 14:10

Mihai Mihalache