Accommodating a second base_url

View: New views
15 Messages — Rating Filter:   Alert me  

Accommodating a second base_url

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm working on a site that can be invoked via two different url's
(mapped to the same directory). The requested url will dictate what top
banner the site uses. Also, because there are likely many places in the
site that $base_url is used, and the url appearing throughout the
session should be that which was initially used, I will need to alter
$base_url.


So, I'm thinking I should just hook_init, and based on the http request,
alter $base_url if needed, and do a variable_set that I later retrieve
in the theme to alter (or not) the background-image used as the top
banner. Is this the best approach?


Jeff


Re: Accommodating a second base_url

by Jamie Holly :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Depending on the server setup you could just use $_SERVER['HTTP_HOST']
to figure out your $base_url. You can even do it in settings.php. It
does depend on if your sites use www. or not, or even allows both www.
and not. You could use a little PHP logic in settings.php to figure out
exactly what the HTTP_HOST should be (stripos, strstr, switch case, etc)
and then define a constant for it (say CURRENT_SITE), then in your
page.tpl.php file a little PHP magic:

<duv id="banner" style="background-image:url(images/<?php echo (
CURRENT_SITE=='example.com')?'site1header.jpg':'site2header.jpg'; ?>)">

That way CURRENT_SITE wouldn't matter if you have www.example.com or
example.com. Of course you would set your base_url off of which ever
they are coming from.

That would work for most server setups (I've been doing something
similar on a pretty big site for over a year now). The only real time
you run into problems is on some proxy front ends.

Jamie Holly
http://www.intoxination.net 
http://www.hollyit.net



Jeff Greenberg wrote:

> I'm working on a site that can be invoked via two different url's
> (mapped to the same directory). The requested url will dictate what top
> banner the site uses. Also, because there are likely many places in the
> site that $base_url is used, and the url appearing throughout the
> session should be that which was initially used, I will need to alter
> $base_url.
>
>
> So, I'm thinking I should just hook_init, and based on the http request,
> alter $base_url if needed, and do a variable_set that I later retrieve
> in the theme to alter (or not) the background-image used as the top
> banner. Is this the best approach?
>
>
> Jeff
>
>
>  

Re: Accommodating a second base_url

by Laura Scott-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What if you simply created different folders in /sites and using  
specific themes for each one, each with its own particular banner? At  
first blush, this would seem to do the trick, yes?

Laura

On Oct 29, 2009, at Thu 10/29/09 4:54pm, Jeff Greenberg wrote:

> I'm working on a site that can be invoked via two different url's  
> (mapped to the same directory). The requested url will dictate what  
> top banner the site uses. Also, because there are likely many places  
> in the site that $base_url is used, and the url appearing throughout  
> the session should be that which was initially used, I will need to  
> alter $base_url.
>
>
> So, I'm thinking I should just hook_init, and based on the http  
> request, alter $base_url if needed, and do a variable_set that I  
> later retrieve in the theme to alter (or not) the background-image  
> used as the top banner. Is this the best approach?
>
>
> Jeff
>


Re: Accommodating a second base_url

by Daniel F. Kudwien :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> So, I'm thinking I should just hook_init, and based on the
> http request, alter $base_url if needed, and do a
> variable_set that I later retrieve in the theme to alter (or
> not) the background-image used as the top banner. Is this the
> best approach?

Whatever you do, if you think you need to alter $base_url, then you are most
likely approaching your task at hand wrongly.

Drupal usually sets up $base_url correctly and you should not alter that
value.  If you need conditional settings per site, then you should look into
Drupal's multi-site support.

sun


Re: Accommodating a second base_url

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Laura wrote:
> What if you simply created different folders in /sites and using
> specific themes for each one, each with its own particular banner? At
> first blush, this would seem to do the trick, yes?
>
> Laura
Hi Laura,
It's one banner used throughout, so setting a variable accordingly will
do the job.
Jeff

Re: Accommodating a second base_url

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel F. Kudwien wrote:

>
> Whatever you do, if you think you need to alter $base_url, then you are most
> likely approaching your task at hand wrongly.
>
> Drupal usually sets up $base_url correctly and you should not alter that
> value.  If you need conditional settings per site, then you should look into
> Drupal's multi-site support.
>
> sun
>
>  
Daniel, don't know if I agree with that. If two url's map to the same
directory, how would Drupal know which one to use? I would think it's
using the domain it was installed on. Would its value change based on
the http request? If not, then it has to be done programatically.

Re: Accommodating a second base_url

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jamie Holly wrote:

> Depending on the server setup you could just use $_SERVER['HTTP_HOST']
> to figure out your $base_url. You can even do it in settings.php. It
> does depend on if your sites use www. or not, or even allows both www.
> and not. You could use a little PHP logic in settings.php to figure
> out exactly what the HTTP_HOST should be (stripos, strstr, switch
> case, etc) and then define a constant for it (say CURRENT_SITE), then
> in your page.tpl.php file a little PHP magic:
>
> <duv id="banner" style="background-image:url(images/<?php echo (
> CURRENT_SITE=='example.com')?'site1header.jpg':'site2header.jpg'; ?>)">
>
> That way CURRENT_SITE wouldn't matter if you have www.example.com or
> example.com. Of course you would set your base_url off of which ever
> they are coming from.
>
> That would work for most server setups (I've been doing something
> similar on a pretty big site for over a year now). The only real time
> you run into problems is on some proxy front ends.
>
Jaime, so far looks like the cleanest approach I could use. Thanks.

Re: Accommodating a second base_url

by Daniel F. Kudwien :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> If two url's map to
> the same directory, how would Drupal know which one to use?
...
> Would its value change based on the http request?

Yes. http://api.drupal.org/api/function/conf_init/6


Re: Accommodating a second base_url

by Jamie Holly :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Using variable_set is trouble waiting to happen. It opens you up to a
race condition:

User 1 hits site 1 at 7:11:01.05 - hits init and sets the variable which
is written to the database. Processing continues
User 2 hits site 2 at 7:11:01:15 - hits init and sets the variable which
is written to the database. Processing continues
User 1 hits the theme engine at 7:11:01:20 - Now the variable has been
set to site 2 by User 2, so they get the wrong banner.

Since your just changing the header and nothing else, I would do it all
in the page.tpl.php file just checking the $_SERVER['HTTP_HOST']. No
need to mess with settings.php or a module. One file and it could be
done in a single line from my example before (and even handle www.
problems):

<div id="banner" style="background-image:url(images/<?php echo (
strstr($_SERVER['HTTP_HOST'],'example.com'))?'site1header.jpg':'site2header.jpg'; ?>)">




Jamie Holly
http://www.intoxination.net 
http://www.hollyit.net



Jeff Greenberg wrote:

> Laura wrote:
> > What if you simply created different folders in /sites and using
> > specific themes for each one, each with its own particular banner? At
> > first blush, this would seem to do the trick, yes?
> >
> > Laura
> Hi Laura,
> It's one banner used throughout, so setting a variable accordingly will
> do the job.
> Jeff
>
>  

Re: Accommodating a second base_url

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel F. Kudwien wrote:
>
> Yes. http://api.drupal.org/api/function/conf_init/6
>
>
>  
Ok, I see where it would work nicely, normally. The reason I set
base_url in settings, and the reason I think it would fail if I didn't
(and then subsequently altered the setting), is that on both the local
development server and the test server, the domain is a subdirectory off
the domain root, which base_url never seemed to handle.

Re: Accommodating a second base_url

by Khalid Baheyeldin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Leave the $base_url alone. Drupal will figure it out automatically.

You can have one install (single code base), and one database, and don't set $base_url.

Then do something like this in template.php:

<?php

function phptemplate_preprocess_page(&$vars) {
  switch($_SERVER['HTTP_HOST']) {
    case 'site1.example.com':
      $vars['banner'] = 'something1';
      break;
    case 'site2.example.com':
      $vars['banner'] = 'something1';
      break;
    case 'site3.example.com':
      $vars['banner'] = 'something1';
      break;
    case 'site4.example.com':
      $vars['banner'] = 'something1';
      break;
    default:
      $vars['banner'] = 'somethingX';
      break;
  }
}

Then in page.tpl.php do:

<?php print $banner ?>

That is all ...
--
Khalid M. Baheyeldin
2bits.com, Inc.
http://2bits.com
Drupal optimization, development, customization and consulting.
Simplicity is prerequisite for reliability. --  Edsger W.Dijkstra
Simplicity is the ultimate sophistication. --   Leonardo da Vinci

Re: Accommodating a second base_url

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Khalid Baheyeldin wrote:

> Leave the $base_url alone. Drupal will figure it out automatically.
>
> You can have one install (single code base), and one database, and
> don't set $base_url.
>
> Then do something like this in template.php:
>
I think everyone who responded subsequently is missing the point that
the change of $base_url was to address all the places in the content,
blocks, views, etc. that use $base_url to build the url for something,
not only for the banner. The links, if the site is requested as
mydomain2.com/drupalsubdirectory have to all say that, and it doesn't
(in my experience) build the $base_url with a subdirectory
automatically. I didn't know it would change based on the domain
changing, which is good to find out, but that doesn't solve the problem
that it had to be set manually in the first place because of the
subdirectory, and that all the links that use a full url have to use the
requested domain.

Re: Accommodating a second base_url

by Daniel F. Kudwien :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I think everyone who responded subsequently is missing the
> point that the change of $base_url was to address all the
> places in the content, blocks, views, etc. that use $base_url
> to build the url for something, not only for the banner. The
> links, if the site is requested as
> mydomain2.com/drupalsubdirectory have to all say that, and it
> doesn't (in my experience) build the $base_url with a
> subdirectory automatically. I didn't know it would change
> based on the domain changing, which is good to find out, but
> that doesn't solve the problem that it had to be set manually
> in the first place because of the subdirectory, and that all
> the links that use a full url have to use the requested domain.

$base_url contains the domain and the base path (the actual subdirectory).
Actually:

  $base_url = $base_root . $base_path;

(hence, why I linked to the actual conf_init() function that does it)

Drupal figures all of that automatically out for you.  If it doesn't, then
you have a serious problem in your server configuration.  Which in turn
would mean that you are working around an issue that normally does not
exist.

sun


Re: Accommodating a second base_url

by Khalid Baheyeldin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


 $base_url = $base_root . $base_path;

And in addition to that, the base_path() function provides you with
the subdirectory name if that is all you are after.

If you have all the domains/subdomains in Apache's vhosts pointing
to the same directory/subdirectory, then all you need is the code in
template.php. The $base_url, including the subdirectory will all be
the same for all the domains/subdomains.
--
Khalid M. Baheyeldin
2bits.com, Inc.
http://2bits.com
Drupal optimization, development, customization and consulting.
Simplicity is prerequisite for reliability. --  Edsger W.Dijkstra
Simplicity is the ultimate sophistication. --   Leonardo da Vinci

Re: Accommodating a second base_url

by Jeff Greenberg-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel F. Kudwien wrote:

>>    
> Drupal figures all of that automatically out for you.  If it doesn't, then
> you have a serious problem in your server configuration.  Which in turn
> would mean that you are working around an issue that normally does not
> exist.
>
> sun
>
>  
You're right! :-)  Worked right away on the test server. I have a local
server issue to hunt down. Thanks, and thanks to you all for not flaming
me!!