Topic: [Plugin] Subsites/Subdomains  (Read 32185 times)

Pages: [1] 2 3 4   Go Down

#1: 17-Nov-2005, 11:25 AM

Emeritus
xwisdom
Posts: 1,732

Hell everyone,

Sometime I created this little plugin to allow subsites under modx.

To install create an new plugin called Subsites and enable the 'OnWebPageInit' event then copy and paste the following code. Be sure to make the necessary modifications to the source:

Code:
$e = &$modx->event;
$subdomain = 'subdomain.yourdomain.com'; // your subdomain name here
if($e->name=='OnWebPageInit') {
   $s = $_SERVER['HTTP_HOST'];
   if($s==$subdomain && $modx->documentIdentifier==$modx->config['site_start']){
      header('Location: index.php?id=6');
      exit;
   }
   else if($s==$subdomain){
       $modx->config['site_start'] = 6; // your start page here
       $modx->config['error_page'] = 6; // your error page here
   }
}

Once the plugin is installed your next step is to make subdomain.yourdomain.com an alias of www.yourdomain.com.
« Last Edit: 17-Nov-2005, 11:26 AM by xwisdom »
xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#2: 17-Nov-2005, 11:38 AM

Administrator

zi
MODx Special Forces /
Posts: 3,555

May Peace Be On You

WWW
Great work Raymond !!  Kiss

Many people asked for this, and now they will be happy  Roll Eyes

Best regards,

zi

#3: 17-Nov-2005, 04:40 PM

Testers

banzai
Posts: 880

MODx Italia

WWW
Great!!
I think it's a nice feature to include in next release
MODx Websites Showcase 
Add your site to www.modx.it!
-----------------------------------------------------
FREE MODx Templates
www.tattoocms.it
-----------------------------------------------------
VIDEO: whats new in MODx 0.9.5   | VIDEO: MODx PHP Application Framework  |  modx Revolution  

bubuna.com - Web & Multimedia Design

#4: 18-Nov-2005, 08:22 AM

Marketing & Design Team

davidm
MODx evangelist
Posts: 7,073

The best way to predict the future is to invent it

WWW
+1 this is really cool, even if I don't need it (yet Grin)
.: nodeo.net : Pour un web libre, moderne et ouvert ! :: david-molliere.net : Suivez en "live" mes expérimentations et billets sur les CMS et autres applications web :.

*** Forums modxcms.fr Participez à l'élaboration du site MODx francophone ! ***

! Nouveau !  En live, ne manquez pas les news de modxcms.fr sur Twitter   ! Nouveau !

MODx est l'outil idéal pour les developpeurs et webdesigners qui cherchent un framework de gestion de contenu hautement flexible et performant, tout en étant simple d'accès pour les utilisateurs finaux.

Config : Apache 2.2.8 - MySQL 5.0.67 - PHP 5.2.8 | Debian 4.0 (Etch)

Réalisations sous MODx : | pargade-notaires.fr | soleil.info | gican.asso.fr | michelez-notaires.com | amadom.gerondicap.com | jocelyne-violet.net

#5: 18-Nov-2005, 10:32 PM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
Nice one!

I just posted yesterday, and I received the complete solution in a day.

Thanks xwisdom.

Do you mind if I ask a little detailed about the code?

Code:
&$modx->event
That will fetch the current event by reference right?

Code:
if($e->name=='OnWebPageInit') {
Why do we need to check the current event again? I thought it will only be called when event is OnWebPageInit?

Code:
   if($s==$subdomain && $modx->documentIdentifier==$modx->config['site_start']){
      header('Location: index.php?id=6');
      exit;
   }
   else if($s==$subdomain){
       $modx->config['site_start'] = 6; // your start page here
       $modx->config['error_page'] = 6; // your error page here
   }
What is the first if conditional did? Checking the default page start?


Hope I can start learning the current API in modX further, by starting from smaller plugin, and getting used to some other resources, which later lead to modules. Is there anyway to start understanding module better?

Thank you

Sincerely,
Wendy Novianto

#6: 19-Nov-2005, 02:39 AM

Testers

The Man Can!
Posts: 345

WWW
Hi all.  Great plugin Raymond!
I've spent some time playing with it, and combining it with a modified version of Susan's UltimateParent to make a sort of UltimateParentSubdomain.  The idea is roughly:
  • set the subdomains' "root"/landing page,
  • determine the subdomain,
  • find the ultimate parent using the subdomain root ID instead of 0.

The problem is that links aren't rendered with the subdomain. If I go to subdomain.boopaloo.com, the links on the page are for boopaloo.com/subdomain/item1.html, so once the visitor navigates away from the subdomain landing page, they're no longer in the subdomain, which often isn't what's desired, and makes my snippet not work Wink

I'm guessing the solution might be in a plugin that loads on event OnFriendlyURLSettingsRender?, but I can't get anything going.  The plugin could check each link's "ultimate subdomain parent" and apply the appropriate subdomain prefix to the link.

So, if doc2 = subdomain2, then any link to content that is under doc2 gets its link rewritten.  (When using alias paths, it'd also be nice to strip the first alias as well, so instead of subdomain2.boopaloo.com/doc2/folder/content it'd just be subdomain.boopaloo.com/folder/content.  This isn't quite as important but it'd be nice.)

Does that make sense?  Any ideas if OnFriendlyURLSettingsRender is the right thing to focus on, or any tips on how MODx makes the friendly URLs in the first place?  I think this could be relatively simple, but I'm still new and don't know where to look.

Having subdomians function like this would be amazing, and would open the door to some pretty cool stuff kind of like WordPress Multi User.  It would also allow an UltimateParent for subdomains, which would make menus within a subdomian much easier, and even stuff like having a simple snippet to display a different header/logo depending on the current subdomain.  Smiley
Thanks all!
Brett
Need MODx Ecommerce? Try FoxyCart!

#7: 20-Nov-2005, 12:20 AM

Emeritus
xwisdom
Posts: 1,732

Code:
&$modx->event
That will fetch the current event by reference right?
That's correct
Quote
Code:
if($e->name=='OnWebPageInit') {
Why do we need to check the current event again? I thought it will only be called when event is OnWebPageInit?

Note here that we are checking for the name of the even by using $e->name. The plugin is only executed when anyone of the events it is listening to is invoked.

Quote
Code:
   if($s==$subdomain && $modx->documentIdentifier==$modx->config['site_start']){
      header('Location: index.php?id=6');
      exit;
   }
   else if($s==$subdomain){
       $modx->config['site_start'] = 6; // your start page here
       $modx->config['error_page'] = 6; // your error page here
   }
What is the first if conditional did? Checking the default page start?

This will cause the plugin to redirect site_start request to the subdomain site_start page.

Quote
Is there anyway to start understanding module better?

You can have a look at our documentation:
http://modxcms.com/developers-guide.html
xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#8: 20-Nov-2005, 12:31 AM

Emeritus
xwisdom
Posts: 1,732

The problem is that links aren't rendered with the subdomain. If I go to subdomain.boopaloo.com, the links on the page are for boopaloo.com/subdomain/item1.html, so once the visitor navigates away from the subdomain landing page, they're no longer in the subdomain, which often isn't what's desired, and makes my snippet not work Wink


Hi Brett,

Are u usng firendly url paths?
xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#9: 20-Nov-2005, 12:48 AM

Testers

The Man Can!
Posts: 345

WWW
Hi Raymond.  Yes I am.  I'm using alias paths as well.  ...
EDIT: Something goofy is happening with my DNS (or I *really* need some sleep Wink ), but as of 10 minutes ago (and after hours of testing/banging my head against my desk) it does indeed rewrite the URLs to include the subdomain.  Sorry to be stupid on this one.

EDIT again:  I think I figured it out.  Seems like once a page is loaded that is "unfriendly" (ie. /index.php?id=6), the subdomains no longer get attached to the links on the page.  I have a weblink that points to one of its children, and I made that link "unfriendly"; so once that link like was clicked, the subdomain stopped appearing in the links.  Changing the weblink to the appropriate friendly link works perfectly.  Hopefully this helps somebody.  Took me long enough to figure out.

Now I can focus on trying to get it to abstain from including the subdomain in links to content that's "outside the subdomain" (as described above)... don't hold your breath Smiley
Thanks again Raymond.  -Brett
« Last Edit: 20-Nov-2005, 08:47 PM by The Man Can! »
Need MODx Ecommerce? Try FoxyCart!

#10: 20-Nov-2005, 11:59 PM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW

Quote
Code:
   if($s==$subdomain && $modx->documentIdentifier==$modx->config['site_start']){
      header('Location: index.php?id=6');
      exit;
   }
   else if($s==$subdomain){
       $modx->config['site_start'] = 6; // your start page here
       $modx->config['error_page'] = 6; // your error page here
   }
What is the first if conditional did? Checking the default page start?

This will cause the plugin to redirect site_start request to the subdomain site_start page.


I still don't get it.

What I can interpret, if domain requested equal to $subdomain, then set the header to the desired doc id, and exit; else, if the the requested domain equal to $subdomain, then it will set the config vars of site start and error page into desired doc id.

From what I assume, the else if statement is the one that do the job, by changing the config site start and page error doc id, then what is the use of the first if statement? Why don't we just move the header() into else if statement, and remove the if statement.

Sorry, if I ask something stupid.

Is there any possibility to include array support as the subdoain vars, and the plugin will loop throught the array of subdomain to determine whether the requested subdomain to be directed to the appropriate doc id that was assigned in the array. In fact, we can set the array key as the subdomain string, and let the value of array as the doc id for start page and error page, but I'm not to for sure whether php allowed an array key with a dot.

Thanks again for the help and reply.

PS: The developers docs is imrpoved from the one that I read a few weeks ago. Awesome! It helps me understand the way MODx work more than I know previously. Thank you guys!

regards,
Wendy Novianto

#11: 21-Nov-2005, 08:23 PM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
Bump!!

Just wanna know, whether someone can help me clarify things up.

Thank you...

#12: 21-Nov-2005, 08:48 PM

Emeritus
xwisdom
Posts: 1,732

Bump!!

Just wanna know, whether someone can help me clarify things up.

Thank you...

Hi wendy,

What is it that you need to be clarify?

xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#13: 22-Nov-2005, 11:29 PM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
xwisdom...

I tried to modified your code, and it ended up not doing what I want it to be. With the Location header, it will only redirect the user, and it will change the url to the user.

Here is the code:
Code:
$e = &$modx->event;
// Main Site Domain
$mainSiteDomain = 'main.domain.com';
// Sub Sites Configuration Array
$subSites[0]['domain'] = 'subdsite.domain.com'; // SubSite Domain (Mirrored to MODx installation domain)
$subSites[0]['alias'] = 'subdsite'; // SubSite Folder Document Alias
$subSites[0]['id'] = 6; // SubSite Folder Document ID
$subSites[0]['error'] = 7; // Error Page ID

if($e->name=='OnWebPageInit') {
  // Extract Domain and URI
  $incomingDomain = $_SERVER['HTTP_HOST'];
  $incomingPath = $_SERVER['SCRIPT_NAME'];
  $incomingQueryString = $_SERVER['QUERY_STRING'];;
  // Checking subSites Array and directing users to the right start page
  for($i = 0; $i < count($subSites); $i) {
    if($incomingDomain==$subSites[$i]['domain'] && !isset($domainRedirectSign)) {
      //$modx->config['site_start'] = $subSites[$i]['id']; // start page here
      //$modx->config['error_page'] = $subSites[$i]['error']; // error page here
      header('Location: http://'.$mainSiteDomain.'/'.$subSites[$i]['alias'].$incomingPath.'?'.$incomingQueryString.'&domainRedirectSign=1');
      exit;
    }
  }
}

Basically I'm trying to change all the address into the main.domain.com/subsite/URI_REMAINING_ADDRESS. I assume that each subsite will be aliased and using all the SEO tools provided by MODx.
The current solution that doesn't fit me is the appearance of subsite on the subdomain. Instead of having subsite.domain.com/home, it will show up as subsite.domain.com/subsite/home.

Is there anyway to use mod rewrite engine to determin the requested domain and rewrite it to the maindomain/subsitefolder?


Sincerely,
Wendy Novianto

#14: 23-Nov-2005, 12:46 AM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
I've been researching about mod-rewrite, and here is what I come up with, I just make a subfolder under my main modx folder, and create a new subfolder again for my other subdomain/domain. In each subfolder, I will have .htaccess file to control the redirection for that subdomain/domain.

Here is what the folders looks like
modx/
  assets/
  manager/
  subsites/
    subdomain1.domain.com/
      .htaccess
    domain2.com/
      .htaccess
  index.php
  .htaccess

Inside the .htaccess of each sub sites I paste this code: (ex: subdomain1.domain.com)
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.subdomain1.domain.com$
RewriteRule ^(.*)$ http://main.domain.com/subdomain1.domain.com/$1 [L,QSA]

I'm assuming that all the SEF options are being activated. The way how the subsites was organized inside the MODx is by seperating them in different directories and aliased the main directory of the subsites into its domain name, for example "subdomain1.domain.com".

So far it works just fine, but I'm having the same problem with the previous one, which is url address on the web browser is changing, from "http://subdomain1.domain.com/home.html" into "http://main.domain.com/subdomain1.domain.com/home.html".

Any other idea or solution? This is areally hacky solution, if xwisdome or somebody else able to handle this in more MODx way, please do so. The one that xwisdome came up with is a really good solution, except, the "subdomain1.domain.com" is being included on every subsites. So in my example above, the result will look like this, from "http://subdomain1.domain.com/home.html" into "http://subdomain1.domain.com/subdomain1.domain.com/home.html".

If the middle part of "subdomain1.domain.com" can be removed, than it will be awesome....

Thank you guys


Sincerely,
Wendy Novianto

Btw, don't use the code given above, it's not working correctly.

#15: 23-Nov-2005, 09:24 AM

Emeritus
xwisdom
Posts: 1,732

xwisdom...

I tried to modified your code, and it ended up not doing what I want it to be. With the Location header, it will only redirect the user, and it will change the url to the user.

Hi Wendy,

You code aboves does exactly what it was coded to do. It will only redirect the user the the address specified.

What I think you're trying to do is have the system "Transfer" all request for a subdomain to subdomain/subsite folder, correct? This solution (I think) can only be accomplished via mod_rewrite.
xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#16: 23-Nov-2005, 10:22 AM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
Hi Wendy,

You code aboves does exactly what it was coded to do. It will only redirect the user the the address specified.

What I think you're trying to do is have the system "Transfer" all request for a subdomain to subdomain/subsite folder, correct? This solution (I think) can only be accomplished via mod_rewrite.

That's so true xwisdom. After spending 5 hours last night, I can't think of any other way to hide the main domain redirection, as well as the the alias path, that is why I'm settling up with those solution aboved, which use empty folder and place htaccess on it, the only problem is, I couldn't get the modrewrite engine to hide the url. It's succesfully transfer the request to the main domain, but the address in URL was changed as well.

My knowledge of ModRewrite and Regular Expression is very limited, so if you can help me with this, it will be great.

Thank you


Regards,
Wendy Novianto

#17: 23-Nov-2005, 10:46 AM

Testers

The Man Can!
Posts: 345

WWW
Hi Raymond and Wendy,
I've read and reread the posts and still am not sure I'm on the same page:

Wendy, is what you're looking to do to strip the "subdomain" alias from the alias path, so instead of:
subdomain1.domain.com/subdomain1/folder/content.html
it reads:
subdomain1.domain.com/folder/content.html?
It seems like using individual physical subdomain folders with individual htaccess files only compounds the problem, but I could be totally wrong.

Raymond, if that's the case (above), is that something that could be put into a plugin?  A mod rewrite could redirect, but MODx would still generate the links with that redundant alias path...

I could be totally misunderstanding the whole situation though! Smiley
-Brett
Need MODx Ecommerce? Try FoxyCart!

#18: 23-Nov-2005, 10:51 AM

Emeritus
xwisdom
Posts: 1,732

Hi Brett,

I think it's possible for mod_rewrite to redirect and then you can use a plugin to process the redirected url.

The problem is finding a way for mod_rewrite to redirect the page to modx so the plugin can work on it.

I'm not a mod_rewrite expert.
xWisdom
www.xwisdomhtml.com
The fear of the Lord is the beginning of wisdom:
MODx Co-Founder - Create and do more with less.

#19: 23-Nov-2005, 11:29 AM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
Raymond, if that's the case (above), is that something that could be put into a plugin?  A mod rewrite could redirect, but MODx would still generate the links with that redundant alias path...

To clarify things, do you mean it like this

For example we have:
- I setup a directory (dir id = 2) under the root dir, and set an alias to be "subdomain".
- All the documents/folders listed under doc/dir id 2 will be the sub sites content.
- I setup "subdomain.domain.com" to mirrored "domain.com"

With the plugin given above by xwisdom, when the user called "subdomain.domain.com" in their url, it will return a different starting page with the "domain.com". Later after that, when you follow the relative link on "subdomain.domain.com", it will do the same thing as "domain.com", which means, "subdomain.domain.com/subdomain/home.html" will be the same as "domain.com/subdomain/home.html", except the one on subdomain.domain.com has a different config of start_page and error_page.

If you're looking for this kind of solution, the plugin is done, and it was listed on top of this post.

But what I'm looking for is the one that you stated, which is to removed the subdomain part, if they are accessing the page using "subdomain.domain.com". So instead of "subdomain.domain.com/subdomain/home.html", they will have "subdomain.domain.com/home.html", but if they use "domain.com", then it will be "domain.com/subdomain/home.html".

The solution that I come up with is using mod_rewrite, to rewrite the "subdomain.domain.com/home.html" into "domain.com/subdomain/home.html".

Hope that clear things up.
I know I'm not that good at explaining something, so pardon me, if it sounds confusing to some of you guys.


Sincerely,
Wendy Novianto

#20: 7-Dec-2005, 08:01 AM

itsawebthing
Posts: 56

This all sounds interesting. I'm a ModX newbie so some of this is currently over my head. Have just installed ModX for the first time.
Wanting to have one ModX install for all my separate domains that I have on one server I maintain (not subdomain.domain.com but where each domain is unique - domain1, domain2, etc.). All the verbage on subsites is a little confusing and sounds like it's still being worked out. I assume that will need to use mod_rewrite and .htaccess. Is there any more documentation on what I want to do?
Pages: [1] 2 3 4   Go Up
Skibo11 and 1 Guest are viewing this topic.