Zend append file and multiple googlemaps keys

Hello all,

I’m not sure how to deal with this, we have a website that uses this on our contact view that has a google map inside.

<?php $this->headScript()->appendFile('http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA21rOAbdxvpahDn8C_9rmuhTNfA3AM-SblUIyYJq_tR97zHPIkhRy2zz1YsiNp9Yb5TdjsjqFYcSw&sensor=false');?>

But, this is for the domain: www.example.com and, I also need to make it work with www.example.pt ;

But we have to have a key per domain name, so I’ve found this solution here:

document.write([
  '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=', {
    'domain1.com': 'apikey1',
    'domain2.com': 'apikey2',
    'domain3.com': 'apikey3',
    'domainN.com': 'apikeyN'
  }[window.location.host],
  '" type="text/javascript"><\\/script>'
].join(''));

I believe the last <\/script> it’s a typo on google docs about this? And should be </script> only? Or Am I wrong?

This, will generate a script tag, but, on our case, we are using zend to Append that information to the head of our view, so, I’m unable to see how can I integrate this solution given by google, and Zend

$this->headScript()->appendFile

in order to have the map to work without warnings on both: www.something.com and www.something.pt .
Can I have your help here?
Márcio

Why can’t I think like that?! :S Seems quite nice.
I will generate the keys for http://example.com and http://example.pt (without the www), they will work with www, so your solutions seems to be valid;

Still one question:
Even if the user doesn’t type www on the browser, will http_host grab the www part?

Thanks a lot, finally I see a light for taking that annoying alert :slight_smile:
Márcio

FWIW, if you use the new Version 3 of Google Maps you don’t need to submit a key any longer.

You’ll have to rewrite just about every line of code you used, but its likely to be a lot shorter, and push you to rely on KML a heck of a lot more than you did previously.

If they have the same content you should definately redirect all domains to one. That way you get all SEO “juice” on one domain, instead of spread out over several.

Another bonus:


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]

Replace the code I showed you earlier with the above to redirect example.com, example.pt and www.example.pt to www.example.com

PS. .pt? Are you from Portugal? If so, your english is really good :tup: :slight_smile:

I always do this in PHP instead of in JS:


$keys = array(
  'www.example.com' => 'apikey1',
  'www.example.pt' => 'apikey2'
);
$this->headScript()->appendFile('http://maps.google.com/maps?file=api&v=2&key='.$keys[ $_SERVER['HTTP_HOST'] ].'&sensor=false');

:slight_smile:

As for the <\/script> part, I believe that is correct, since you writing just </script> generates a HTML error.

I see. :slight_smile: That’s quite a travel for a google map key issue. But I will give it a try right away.

Thanks a lot for your time, again. :slight_smile:
Márcio

Scallio - it worked like a charm. :slight_smile: And know that I think of it, probably even one google map key would be enough if we use .htaccess to redirect all the access from www.example.com or example.com to example.pt for example.

But I’m glad like it is. Thanks a lot for the .htaccess bonus. :smiley:

Márcio

It’s not just for google maps, it’s recommended by google for SEO as well afaik (make either the www or the non-www version of your domain the canocical domain).

You could of also use (if you don’t feel like fiddling with the .htaccess stuff):


$keys = array(
  'www.example.com' => 'apikey1',
  'example.com' => 'apikey1',
  'www.example.pt' => 'apikey2',
  'example.pt' => 'apikey2'
);

$this->headScript()->appendFile('http://maps.google.com/maps?file=api&v=2&key='.$keys[ $_SERVER['HTTP_HOST'] ].'&sensor=false'); 

or use any of the preg_* functions to find the domain name, or strrpos, or substr solutions, or …
However, I’d personally recommend the .htaccess aproach to direct non-www to www, or the other way around if that has your preference :slight_smile:

If the user doesn’t type www HTTP_POST won’t take the www part. But it’s always advisable to have one 301 redirect to the other using .htaccess (if on Apache) or ISAPI_Rewrite (if on IIS)

The code for Apache is as follows (code for ISAPI_Rewrite is similar I believe):


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=301]

To redirect all non-www requests to www-requests (like SitePoint does).

I really need to read more about SEO myself, I’m quite ignorant on that matter.

I prefer to learn fishing. But I will not refuse the fish, if that don’t stop me from learning. :slight_smile: Thanks a lot.

Yes I am. :slight_smile: But I believe it isn’t a good english at all. :slight_smile: Really. Is no fake modesty or something.
But even in Portuguese I could be writing so much better. But it’s a quest that not many can do. Still, and anyway, I believe it’s enough to write here, and clarify my doubts.

ps - You seem to be a non-native English speaker yourself. :wink: Fellow european. :slight_smile: Oh well… :slight_smile: S’on peux pas parler le francais dans tout l’europe, alors… ça nous reste que le faire en anglais, je soupçonne :slight_smile:
Voilà, un bonus. :slight_smile: