English only, Tech
Schreibe einen Kommentar

Get correct image urls with WPML, W3TC, CDN on Nginx

Here comes another tech post. Again in English.

After testing many possible ways to manage content in different languages, I’m pretty sure WPML is the best WordPress extension on the market. However, when moving Placedise to the next level, I recently came across several problems. They are all based on the fact that WPML changes the url of images if you surf translated content. In my case, I use subdirectories for other languages – e.g. German sites go to www.domain.com/de/ (but I think it’s the same if you use domains or parameters).
The problems occur if you use Nginx rather than Apache and additionally if you use a Content Delivery Network (CDN). Both are clever moves if you want to optimize performance, but WPML not working is not acceptable.

Since I didn’t found that much helpful information on the internet, I will write down my solutions here. Feel free to be happy about it!


Issue 1: You always get redirected to subdirectory language if two pages share the same slug

For example, you have the default blog page at www.domain.com/blog/ and the translated (German) one at www.domain.com/de/blog/. Somehow, the page always redirects you to the German version.

One solution would be to simply ensure that there are no identical slugs, but that would be very bad for user as well as SEO issues. The solution, however, is pretty easy! The bug usually appears due to a caching problem with fastcgi.
Add the following line to the .conf-file into your server block and you are fine:


Issue 2: Images in subdirectories won’t display with WPML on Nginx

In case you experience this bug, you need to rewrite the urls correctly. With Nginx, you can’t simply change your .htaccess file as you would in Apache. So you need to edit the .conf-file that gets loaded for the relevant server block (maybe you already have parts of it). Add/Adjust:


Issue 3: Images in non-default languages won’t display with WPML if you use a CDN

That’s also a common problem. The reason is again pretty simple: You upload your files to your CDN with the „real“ url. WPML changes the urls on your website. However, since the rewriting part (see issue 2) doesn’t apply to the CDN, your site won’t find the files, if the path suddenly changes from www.domain.com/images/img.png to something like www.domain.com/de/images/img.png.

The easiest and fastest way to solve this problem is to change the way the urls are rewritten for the CDN. I use the W3 Total Cache plugin. There, I simply hooked into the rewriting filter and kicked out the language-part. Add to your theme’s functions.php:


That’s it. Everything works and you can use WordPress with WPML, W3TC, CDN on Nginx the way you want.

Happy coding!

Kategorie: English only, Tech
Bewerte diesen Beitrag!
Get correct image urls with WPML, W3TC, CDN on Nginx
4.9, 9 Votes

Der Beitrag gefällt dir? Teile ihn!
Hier noch eine kleine Werbeanzeige. Wenn etwas interessantes dabei ist, unterstützt ein Klick (wie das mit Werbung so ist) natürlich den Blog.