• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Domain routing possibilities in CI?

#1
[eluser]stevefink[/eluser]
Hi all,

Super quick question.

I have a few URLs that look like the following:

http://www.example.com/users
http://www.example.com/admin
http://www.example.com/partners

I understand how URI routing works on CI's internal engine in routes.php in order to be able to manipulate matters, however -- is it possible to use mod_rewrite + CI routing to have users point to say:

http://users.example.com

admin to http://admin.example.com .. etc? All under one CI install?

if I just setup my mod_rewrite rules properly will it know which controller is responsible for handling the mapping?

Thanks!

#2
[eluser]Pascal Kriete[/eluser]
Sure it is, but it's ... iffy. The mod rewrite isn't a pretty one. This should get you started:

Code:
RewriteCond %{HTTP_HOST} ^(admin|users|partners).example.com$
RewriteRule (.*) /index.php/%1/$1 [L]

#3
[eluser]Sean Murphy[/eluser]
Sure! Something along these lines should work:
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(users|admin|partners)\.
RewriteRule ^(.*)$ /index.php?/%1/$1 [L]

#4
[eluser]stevefink[/eluser]
Thanks for the reply!

Would these rules have to precede the default .htaccess rules which remap all to index.php?

#5
[eluser]Sean Murphy[/eluser]
Yes, and
Code:
RewriteRule ^(.*)$ /index.php?/%1/$1 [L]
would be used instead of something like
Code:
RewriteRule ^(.*)$ index.php/$1 [L]
If you'd like to post the htaccess file you're currently using, we can tell you exactly what to change.

#6
[eluser]stevefink[/eluser]
Sure, I'm cleaning this up currently -- my mod_rewrite is extremely rusty .. but it looks something like this thus far:

Code:
RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{HTTP_HOST} ^admin.example.com$
    RewriteRule ^(.*)$ /index.php/admin_account/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    # Redirect adding leading www to domain or any subdomain if missing
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]

Thanks again. :-)

#7
[eluser]Sean Murphy[/eluser]
I think this will do it.
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^admin.example.com$
RewriteRule ^(.*)$ /index.php/admin_account/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(users|admin|partners)\.
RewriteRule ^(.*)$ /index.php?/%1/$1 [L]

# Redirect adding leading www to domain or any subdomain if missing
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
This section might cause a problem though (for admin routing to /admin):
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^admin.example.com$
RewriteRule ^(.*)$ /index.php/admin_account/$1 [L]

Glad I could help!

#8
[eluser]stevefink[/eluser]
Hey Sean!

Thanks for the help. Unfortunately it's a no go, but I've enabled RewriteLog and going to go through it step by step to see why it's not catching the first condition set. Truly odd! It could be the way I have VirtualHosts setup, so I'll need to dig in deeper to make sure it's properly rendering %{HTTP_HOST} (the rules might be correct, or they might not.)

Thanks again.

#9
[eluser]Sean Murphy[/eluser]
Sorry, I haven't tested it. BTW,
Code:
RewriteRule ^(.*)$ /index.php?/%1/$1 [L]
should be
Code:
RewriteRule ^(.*)$ /index.php/%1/$1 [L]
(No question mark) Sorry 'bout that.

#10
[eluser]stevefink[/eluser]
I've also tried the following, unfortunately just returns HTTP status code 500:

Code:
RewriteEngine On
    RewriteBase /
    #RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{HTTP_HOST} ^admin\.example\.com$ [NC]
    RewriteRule ^.*$ index.php/admin_account [L]


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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