• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Routing by Domain?

Is it possible to write a routing rule that looks at the subdomain? I have an application that has a public facing side (www.application.com or application.com) and a private side (userdefined.application.com, myname.application.com) where the subdomain is specified by the user.

What I'd like to do is route all traffic to the public facing app (www or no subdomain) to a specific controller while routing all other traffic with user defined subdomains to a different controller. Is this possible? I've tried adding route definitions with domain info but they don't work and I'm not sure if it's because CI disregards domain info while checking routing rules or if my regex just sucks.

Any help would be appreciated.



Here's what I've tried so far:

/* reserved routes */
$route['default_controller'] = "app";
$route['scaffolding_trigger'] = "scaffolding";

/* handle routes for public / www site */
$route['http://www.mydomain.local/(:any)'] = "/web/$1";
$route['http://mydomain.local/(:any)'] = "/web/$1";

/* handle routes for ajax */
$route['/ajax/(:any)'] = "/ajax/$1";

/* handle all routes for app */
$route['(:any)'] = "/app/$1";

[eluser]Phil Sturgeon[/eluser]
The routes are applied to the URI only. That is basically the controller, method and any URL segments (method parameters).

You want to do some mod_rewrite for this as that does have access to the host.

<IfModule mod_rewrite.c>
# Redirect to user blog (with any trailing path)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/blog/user/$1$2 [R=301,L]

## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]


That is an old example I posted up a while back. Should cover most of your CodeIgniter site needs. The main bit for you to pay attention to is the top block. Change the domain name and the path it should go to on successful match.

Awesome! Thanks for the quick reply. I'll take your examples and give it a shot.



Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.