Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Wildcard Domain. Handle in .htaccess or in Fuel?
  • I'm building a site where users will be able to have their own "website". There will be a root domain (Let's say root.com) and clients get to use their own domain (say client.com) which will point to us at root.com I want to setup a wildcard virtualhost so any domain will point at the fuel installation. What I can't decide is where I should do a check for how they are accessing the site. Should I use .htaccess and mod_rewrite to do something like
    RewriteCond %{HTTP_HOST} !root.com [NC]
    RewriteRule .* index.php/clients/%{HTTP_HOST} [L]
    
    and have all client site requests go through one controller? Or should I have every controller detect the http_host and decide what to load. Eg, if the uri is /about, root.com will probably have an about page, but client sites will most likely also have an about page. Deciding if the client's domain is an active account or not could be done with a DB lookup, or there could be a RewriteMap file with domain > client_id mappings. I'm assuming the map file would be faster here but I've never actually tested that. I'm sure there are other options and I'd love to hear them.
  • It depends on how the application is designed, and what functionality you're offering. If it is entirely modular, I would go for the single front controller. It can do it's thing based on http_host, and pass required customisation info on the modules to be loaded. If it's not modular (i.e. controllers in app), I think it's better to leave it as-is, have some central location that does the detection and the prepping (maybe an always_load class with an _init method), and deal with it in the controllers. Which imho is a suboptimal design, I would go for the modular approach. mod_rewrite is going to be faster than PHP, and you don't waste a server connection. Also, move the rules into your virtual host, then disable .htaccess functionality for that virtualhost. It will save Apache doing lots of file I/O to read .htaccess files.
  • So far I have a virtualhost setup as a catch all, sending everything to fuel. Then I'm using this rewrite setup
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} !^root.com$ [NC]
    RewriteCond %{HTTP_HOST} !^www.root.com$ [NC]
    RewriteRule .* index.php/site/index/$0 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php/$1 [L]
    
    So anything from a client domain automatically gets sent to the Site controller's index action. I'm guessing I could just use HMCV to access other controller from here. Is this too messy? Anyone see a better way?
  • This is how I would do it (apart from the map discussion to catch non-existent host names). What has to go into the site controller depends on what your application exactly does, I can't comment on that.
  • I am currently doing something like this and this is how I made it work: - Domain is requested and uses the central index.php through a symlink (I prefer that because that way I could still do some modifications per domain if necessary by replacing it with a specific index.php)
    - Among my always load rules is my "Base" package that is loaded first of all the packages and makes a call to my Init class in its bootstrap
    - My Init class detects the domain and starts modifies the cache path and active DB and loads other domain specific configs
    - From there it's the same for all domains, but based on their specific configs The Init class might easily be in the app with the Init class as an always_load class instead of through an always_load package's bootstrap, I just chose to write this in a way I might reuse among apps.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion