Hosting Admin URLs on a Subdomain

by nate
Quick tutorial on how to host admin routes on a subdomain.
Admin routing is one of those features in Cake that turns out to be a lot handier than you think it will. I use it all the time, but something about it bothers me.

It's probably just my own paranoia, but hosting the administrative backend of a site on the same domain as the site itself has always felt wrong to me. I doubt the following would improve the security of a site any measurable degree, but (a) it makes me feel better, and (b) it looks nice.

Let's say I'm hosting a new site at example.com, and using Cake's admin routing, I access the admin section at example.com/admin/, but I've also set up admin.example.com, and I want to access it from there instead. When setting up admin.example.com, I want to point it to the same path as example.com itself. Then, in app/config/bootstrap.php, I add the following:

PHP Snippet:

<?php 
if (env("HTTP_HOST") == "admin.example.com") {
    
$_GET["url"] = "admin/" $_GET["url"];
} elseif (
strpos($_GET["url"], "admin") === 0) {
    
header ("HTTP/1.0 404 Not Found");
    die();
}
?>

The first part of the if block adds "admin/" to the URL that is parsed by Cake. The second part disables admin-URL access from any other (non-admin) domains. Optionally, instead of just sending a 404 and dying, you could set $_GET["url"] to a path that you know doesn't exist in your site (i.e. "notfound/"), which will make Cake render your "Not Found" page in production mode.

And that's it. The nice thing about this trick is that you can use it for any kind of URL manipluation whatsoever (the SEO possibilities abound).

Report

More on Tutorials

Advertising

Comments

  • jyrgen posted on 04/16/07 02:20:01 AM
    when i try to make use of mod_rewrite, i get missing controller errors, although i *have* modified bootstrap.php

    ### part of appcontroller dump

    [controller] => AdminusersController
    [controllerName] => Adminusers
    )

    [pageTitle] => Missing Controller

    ### part of appcontroller dump

    the controller name is "Users" and not "Adminusers"

    what can i do ?

    thanks, jyrgen

    • thomasfedb posted on 10/17/08 11:25:48 PM
      when i try to make use of mod_rewrite, i get missing controller errors, although i *have* modified bootstrap.php

      ### part of appcontroller dump

      [controller] => AdminusersController
      [controllerName] => Adminusers
      )

      [pageTitle] => Missing Controller

      ### part of appcontroller dump

      the controller name is "Users" and not "Adminusers"

      what can i do ?

      thanks, jyrgen


      Try changing "/admin" to "admin/" or "/admin/"
  • sourtouch posted on 04/13/07 09:40:14 PM
    This is a great little trick - thank you for sharing.

    I would also really appreciate it if someone could share some ideas on managing multiple domains from one app. Thanks!

    S :)
  • julienmarie posted on 04/11/07 05:30:12 AM
    Hi, I'd like to know how to do it for one app managing multiple domains (multiple sites), with different content for every domain. Thanks a lot.
  • jyrgen posted on 01/17/07 05:26:18 PM
    Though bootstrap.php has just 100 lines,
    i'd love to know where to enter your code.
    thanks a lot !
    jyrgen
  • phirschybar posted on 10/01/06 04:27:15 PM
    Very cool.

    I am making an app w/ Cake where I need to allow users to login at theirChosenSubDomain.myApp.com. Sort of like how BaseCamp does it. Can anybody point me in a good direction on this? I know there will be some server configuration involved in addition to messing w/ Cake to make it happen. Any advice? Basically I would need to route/map theirChosenSubDomain.myApp.com to myApp.com/theirChosenSubDomain...

    Thanks.
    • eblount posted on 10/07/06 01:54:13 AM
      Can anybody point me in a good direction on this?
      So here's what you do:

      First, set up wildcard DNS. In your dns entry, set up *.yourdomain.com to point to the same IP address as www.yourdomain.com. You can find tutorials on wildcard DNS on Google.

      Second, you have to set up Apache for wildcard. Put a ServerAlias *.yourdomain.com directive in the VirtualHost for your site in httpd.conf.

      Third, and I'm basing this completely on this article, you need to add some code to app/config/bootstrap.php. Here's what I would do:

      // if there's a subdomain and it's not www
      if (env("HTTP_HOST") != "www.yourdomain.com" && env("HTTP_HOST") != "yourdomain.com") {
      // filter out the .yourdomain.com part
      $clientdomain = str_replace(".yourdomain.com", "", env("HTTP_HOST"));
      // from the example, add clients/theirdomain/ to the URL
      $_GET["url"] = "clients/".$clientdomain."/" . $_GET["url"];
      }
      ?>

      Then, create a clients controller, and in routes.php, set up /clients/:clientdomain/ so it will pass the client domain to the display method of the controller.

      $Route->connect('/clients/:clientdomain/', array('controller' => 'clients', 'action' => 'display'));

      Access the variable subdomain with $this->params['clientdomain'] from the controller.

      That's it!

      Have Fun!
      Eric
  • ruzel posted on 11/30/99 12:00:00 AM
    I haven't ever worked with cake and I want to put it somewhere safe on my domain (which is already an app making use of modrewrite). So, ideally I'd like to put it in dev.domain.com where the entire thing resides in a folder in the domain's root folder.

    So far I've been able to do this and hook it up to the DB fine but when I dial up the controller... nuthin. I tried this fix as well, changing out 'admin' for 'dev' but Cake comes back with an error that it cannot locate a dev controller. I think I understand why this is happening, but I'm not sure what to do next.
login to post a comment.