Jump to content

Can '/assets/' folder be moved to another domain/subdomain?


Gabi

Recommended Posts

Good day, gentlemen!

Can ProcessWire use an /assets/ folder form a different domain/subdomain, as recommended for static content (http://developer.yahoo.com/performance/rules.html#cookie_free)?

I looked into /index.php and it seems that the paths/URLs are stored as site root + folders (i.e /processwire/ + /site/assets/ for the assets url). The site root gets always prepended to the folders so I couldn't simply reassign the property.

Link to comment
Share on other sites

I once created a pseudo/subdomain CDN like this: I created 10 subdomains cdn1.mydomain.com cdn2.mydomain.com,.. all pointing to the same folder as the mainsite.
In my templates I had some simple logic to output my assets with one of these subdomains (If i remember correctly i used the last number of the page id, which worked quite good in my case).

The only thing you need now is something like this in your .htaccess right after the www-redirect:

  # subdomain CDN for my image assets
  RewriteCond %{HTTP_HOST} ^cdn(.*)$
  RewriteCond %{REQUEST_URI} !^.*\.(jpg|jpeg|png)$
  # all other requests are redirected to the main site to avoid duplicate content:
  RewriteRule ^(.*)$ http://www.mydomain.com%{REQUEST_URI} [L,QSA]
  • Like 2
Link to comment
Share on other sites

Thanks for your answer.

I realize I should've been more specific: my main concern is the content of the /site/assets/files/ folder which is automatically created every time you create a new page. Let's say when you save images belonging to a page. Did you change the location where those images are saved?

I thought it would be easier to just move the entire assets folder, but moving just the /site/assets/files/ would be just as good.

LATER EDIT: I'm obviously a little slower. I realize now that creating subdomains pointing to the same folder allows me to serve them without moving them. But not using the API. Or a real CDN. Am I missing something (again)?

Link to comment
Share on other sites

Sorry for being so cryptic. But you got it right. :)

If you dont need a real CDN and some subdomains on the same server is enough for you my idea should work for you. Maybe this isn't the most professional solution, but its quite fast to implement. 

Link to comment
Share on other sites

@Gabi: this sounded like an interesting idea, so I mocked up a proof-of-concept module (Page Render Relocate Assets) that dynamically alters asset requests to point to another location. This isn't, however, something you should yet consider production-ready.

Idea is quite simple: the module just hooks after page render and alters generated markup, converting any requests to /site/assets/ to another URL specified via module settings. Just to be on the safe side this (at the moment) only alters requests that start with a double quote and /site/assets, ie. "... src="/site/assets/..." would get replaced, while "Look at my /site/assets/!" or "... src="http://example.com/site/assets/..." wouldn't. 

To test this I created a new subdomain and pointed it to my own sites /site/assets/. According to this blog post this should be enough, and so far things seem to work just fine. You can see it in action here: http://www.flamingruby.com/about/. Note that one of the images on that page is (for some CKEditor-related reason) embedded with full URL and thus doesn't get "relocated", but other one (the bike) should point to static.flamingruby.com instead.

.. so to answer to your question here: changing assets location would require a lot of work at the moment, but this way you can serve them without useless cookies etc. as long as that's what you're after. If you're looking to share the load between multiple servers or something like that, this alone won't do the trick (though if combined with some kind of server-side replication that would be quite possible too.)

I might have to take this module a bit further at some point, I'm kind of starting to like where this is going :)

  • Like 5
Link to comment
Share on other sites

ProcessWire wants to have a local path to save files when you upload them. But using a solution like Teppo's, where you alternate the URL where they live, is certainly a possible way to redirect requests for any file to an alternate location. Another possibility is that I could make PagefilesManager::url() hookable, so that you could modify it with a hook. This would probably be the cleanest method, and I'll be happy to add the hook if anyone wants it. However, you would still need to be responsible for making sure that the files that live in /site/assets/files/* manage to find a way to their new URL location, whether it's literally having them copy to that location, or it's a directory mapped to another drive or server, etc. 

  • Like 3
Link to comment
Share on other sites

Another possibility is that I could make PagefilesManager::url() hookable, so that you could modify it with a hook. This would probably be the cleanest method, and I'll be happy to add the hook if anyone wants it.

I'd be very happy if you could do that -- would love to explore this concept a bit further and method I've posted here is nowhere close to "clean".. :)

  • Like 4
Link to comment
Share on other sites

The PagefilesManager::url() method is now hookable on dev. For high volume functions like this, I go a little further than just prepending 3 underscores and instead implement both a hooked and non-hooked version, just to reduce any potential overhead. Meaning, PW's hook system doesn't get called upon unless/until the function is hooked. Not sure it really matters, but it's just a minor optimization. 

  • Like 6
Link to comment
Share on other sites

  • 4 months later...

I'd be very happy if you could do that -- would love to explore this concept a bit further and method I've posted here is nowhere close to "clean".. :)

Hi Teppo, did you manage to move assets/files to another domain?

Link to comment
Share on other sites

  • 3 weeks later...
 I would like to save all assets/files to a different domain, is it possible hooking  PagefilesManager::url()?

It's not necessary here, as ProcessWire doesn't include the http host with url() calls... just the path. So you can prepend the http host yourself, or make a custom function to do it for you. For example, you make an uppercase "URL" property to refer to your own version: 

wire()->addHookProperty('Pagefile::URL', function($event) {
  $event->return = "http://www.domain.com" . $event->object->url;
}); 

Usage:

<img src="<?=$page->image->URL?>">
  • Like 5
Link to comment
Share on other sites

  • 2 months later...

@Manol,

If it was me I'd look at getting a custom module written. I've used something similar for ExpressionEngine which really takes the load off the site - especially when it comes to site backups. The only problem is that it doesn't delete files from S3. I'm not sure if that's a limitation with the plugin or S3, but I know there's a few GB of files up there that aren't being used.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

   I'm developing  patrimonio24.com,  the main purpose is to use  the contents  by a mobile app. This page is hosted in an amazon  EC2 ami, probably there will be peeks where many people will use it at the same time with their mobiles so I decided to copy every night all contents to an S3 bucket which is then pushed to a CDN and read the data from there.

   I'm using s3cmd with a crontab to move and sync all contents at night, so every day users have fresh data, it is working perfectly but a cleaner way would be to directly write all processwire /assets/files directly to the S3 bucket.

   Sometimes I think I'm doing things ok same others I feel like a very beginner, if somebody has done something similar any ideas will help. If somebody is interested in doing something similar and don't know how to start  just ask me.

Nice weekend to everybody.

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...