Marketing & Sales Videos, Mobile and Tablet Marketing, Technology

WordPress Rules Have Exceptions, Too

WordPress made a major evolutionary step forward in the blogging platform, moving it closer to a full-fledged content management system with revision tracking, more support for custom menus, and–the most intriguing feature for me–multi-site support with domain mapping.

If you are not a content management system junkie, it’s okay. You can skip right past this article. But for my fellow techno-geeks, code-heads and apache-dabblers, I want to share something interesting, and something cool.

Multi-site is a feature that allows you to run any number of WordPress websites with a single WordPress installation. If you administer multiple sites, it’s nice because you can install an approved group of themes and widgets, and activate them for your client sites. There are a few technical hurdles to map your domains, but the process is not difficult.

One of the problem areas I identified is in theme customization. Since themes can be made available to multiple websites, any customizations you do to a theme will also affect any other sites using that theme on your multi-site install. My way around this is to duplicate a theme before I start customizing, and clearly name the theme for the client site I am styling it for.

Another interesting issue is what happens in the .htaccess file on your Apache server. WordPress needs to rewrite paths on a blog-by-blog basis and does this with a rewrite rule and a php file.

WordPress uses the following rewrite rule:

RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

Essentially, anything that is in a subdirectory of mysite.com/files/directory gets rewritten to mysite.com/files/wp-includes/myblogfolderpath… and this is where it gets interesting. What happens if you actually need to have a file on your server that is mysite.com/files/myfolder/myimage.jpg? You get a 404 error, that’s what happens. The Apache rewrite rule kicks in and changes the path.

Granted, you might never come across this problem, but I did. I had a site that needed to use a javascript widget from another website, and it needed to find graphics at mysite.com/files/Images/myfile. Since there was no way to change the file on the host site, I needed for figure out a way to do this on my server. The easy solution is to create a rewrite condition that makes an exception for specific files.

Here is the solution:

RewriteCond %{REQUEST_URI} !/?files/Image/file1.jpg$
RewriteCond %{REQUEST_URI} !/?files/Image/file2.jpg$
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

The rewrite conditions have to be placed before the rewrite rule, or this trick won’t work. It should be easy to modify this condition for your own purposes, should you encounter a similar problem. The solution worked great for me, allowing me to substitute custom graphics rather than the less desirable alt text that didn’t suit my design. Hopefully, it will work for you, too.

Leave a Reply