homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Google API fallback to local version using rewrite?

 2:50 pm on Nov 17, 2009 (gmt 0)

When using Google API to deliver things like jQuery I would like to fall back to a local version if Google for some reason fails to deliver (I know it is unlikely, but I like to be safe).
I could do this by testing the existence of the jQuery object in javascript, but I was wondering if it could also be done using rewrites?

I have this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

And a "local" file in the relative path: js/jquery.js

I would like the "local" file to be loaded if the "remote" file is unavailable.



 2:58 pm on Nov 17, 2009 (gmt 0)

There's not any simple way I can think of with with mod_rewrite, because the browser makes the request for the script from the remote site, not through your server...

To do what you are asking I would probably use PHP or some other scripting language to 'check' for the file when the page loads and dynamically switch the URL served to the browser if it's not present or there's an issue.


 3:01 pm on Nov 17, 2009 (gmt 0)

You could write a server-side script in PERL or PHP to do this, if it won't matter that all requests for ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js will be logged as coming from your server instead of from the client.

[clarity] The client-side ajax call would then be modified to go to your server which would then make a request to google, and if that request timed out or otherwise failed, return the local content. [/clarity]



 3:37 pm on Nov 17, 2009 (gmt 0)

Yeah, I was thinking about just putting the request to see if the files was readable on the pages and switching the URL served to the client based on the result, but jdMorgan's way might be better, because you can use the some request for the jQuery script all the time and just switch where the contents of the file are served from based on the result of the attempted connection...

Either way, in PHP I would probably use is_readable() to make the determination and remember if you go with jdMorgan's you will either have to parse JS as php (or whatever scripting language you decide on) or set the jQuery file location as .ext (php, pl, etc.) to get the script to run before the content is delivered.


 4:58 pm on Nov 17, 2009 (gmt 0)

Ok, thanks for the help. I guess I stay with the pure javascript solution then. I was just wondering if it could be done with a mod_rewrite.


 10:25 am on Nov 26, 2009 (gmt 0)

Keeping in mind that this rewrite stuff just doesn't seem to agree with me what I am about to say is probably stupid.
But why wouldn't this work: (it is sort of in pseudo-code but you get the general idea?)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js$ ^js/jquery.js$


 4:01 pm on Nov 26, 2009 (gmt 0)

For two reasons:

1) RewriteRule cannot 'see' the protocol or domain name -- It looks at only the localized requested URL-path. You'd need to use RewriteConds to examine those other parts of the request.

2) Unless you own and control "googleapis.com", then your code can have no effect, because the query.min.js script is requested by the client (e.g. browser) from that server, not from yours.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved