Jump to content

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


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;


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

  • 2 months later...


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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By AndZyk
      we have many projects as pages containing images inside regular image fields and repeater matrix elements. Now we would like to organize our assets.
      ProcessWire saves assets with id as folder name and makes separate folders for repeater matrix elements.
      Is there a way to collect all assets of one page and export them in a folder with the page name/title?
      I think I could achieve this by using the command line wget for this website or maybe by a hook. But maybe someone has done this before. 😉
      Regards, Andreas
    • By horst
      Hi, on a site I want to disable access to original images and only allow to access thumbnails and watermarked image variations.
      A good solution for protecting original images can be found a bit down in this thread:
      Old content of this initial post:
    • By vmo
      this has resolved.
      Thank you
    • By John W.
      Summary: If I have a site sitting on a server called testing.com, then on the server change it to mycompany.com, do I have to modify any settings in ProcessWire?
      After building several processwire for clients, the one question that I haven't had is about moving a processwire site from an old (temporary) domain to a new domain.
      For instance, I typically setup development sites for clients and register a temporary domain (and lock with a password). For instance, thee client can go test.com and get all their content finalized, change wording, add news, swap out photos -- everything to make the site production read.  
      Next, on my hosting account I can have testing.com renamed to theircompany.com - and change the nameservers.
      A simple question here. If I have a hosting company rename the account from testing.com to theircompany.com, change my nameservers whereas going to theircompany.com loads the production site, Is there anything I need to change in the processwire configuration or in the PW database?
      In the past, I've just manually copied the site, and fresh-installed PW, and imported the data. Seems like a lot of hassle.
      I have a second part question about updating the PW core, however, after consideration I'll post it as a new topic.
    • By EmailForMyWork
      Hi All!
      Currently Processwire saves the files in the assets directory as follows assets > files > pageid > jpg, doc, mp4, etc.
      But what happens I have a project that we developed 2 years ago and currently uses 2 TB of space because the majority of content is video, so I thought that if Processwire could save the files per year, it means that the structure is thus assets > files > year > Pageid > jpg, doc, mp4, etc.
      This in order to be able to create an NFS volume for each year and that the volume does not grow in an exaggerated way, since the bigger the volume, the longer the reading and writing times in that volume.
  • Create New...