Welcome Guest, Not a member yet? Register   Sign In
Routing by Domain?
#1

[eluser]craigkoster[/eluser]
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.

Thanks,

Craig
#2

[eluser]craigkoster[/eluser]
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";
#3

[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.

Code:
<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]

</IfModule>

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.
#4

[eluser]craigkoster[/eluser]
Awesome! Thanks for the quick reply. I'll take your examples and give it a shot.

Regards,

Craig




Theme © iAndrew 2016 - Forum software by © MyBB