Content Marketing

WordPress .htaccess 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 mapping your domains, but the process is not difficult.

One of the problem areas I identified is 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]

Broken down, this means:

  1. RewriteRule – This directive tells Apache that this is a rewrite rule.
  2. ^([_0-9a-zA-Z-]+/)? – This is a regular expression (RegEx) that matches a string of characters that starts with an optional string of alphanumeric characters and hyphens followed by a slash. The parentheses indicate a capture group, which means that the matched text can be used in the replacement string.
  3. files/ – This matches the string “files/”.
  4. (.+) – This is another capture group that matches any string of characters, one or more times.
  5. wp-includes/ms-files.php?file=$2 – This is the replacement string that replaces the matched string. It tells Apache to redirect the request to “wp-includes/ms-files.php”, with the value of the second capture group ($2) as a query parameter called “file”.
  6. [L] – This is a flag that tells Apache to stop processing any further rules if this rule matches.

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]

Broken down:

Line 1:

  1. RewriteCond – This directive tells Apache that this is a RewriteCond rule.
  2. %{REQUEST_URI} – This is a server variable that contains the path of the requested URI.
  3. ! – This is a negation operator that means “not”. It is used to invert the condition.
  4. /?files/Image/file1.jpg$ – This is a regular expression that matches the exact string “/files/Image/file1.jpg” at the end of the requested URI. The question mark and forward slash before “files” make the leading slash optional.

Line 2:

  1. RewriteCond – This directive tells Apache that this is a RewriteCond rule.
  2. %{REQUEST_URI} – This is a server variable that contains the path of the requested URI.
  3. ! – This is a negation operator that means “not”. It is used to invert the condition.
  4. /?files/Image/file2.jpg$ – This is a regular expression that matches the exact string “/files/Image/file2.jpg” at the end of the requested URI. The question mark and forward slash before “files” make the leading slash optional.

Line 3:

  1. RewriteRule – This directive tells Apache that this is a rewrite rule.
  2. ^([_0-9a-zA-Z-]+/)? – This is a regular expression that matches a string of characters that starts with an optional string of alphanumeric characters and hyphens followed by a slash. The parentheses indicate a capture group, which means that the matched text can be used in the replacement string.
  3. files/ – This matches the string “files/”.
  4. (.+) – This is another capture group that matches any string of characters, one or more times.
  5. wp-includes/ms-files.php?file=$2 – This is the replacement string that replaces the matched string. It tells Apache to redirect the request to “wp-includes/ms-files.php”, with the value of the second capture group ($2) as a query parameter called “file”.
  6. [L] – This is a flag that tells Apache to stop processing any further rules if this rule matches.

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.

Tim Piazza

Tim Piazza is a partner with Social LIfe Marketing and the founder of ProSocialTools.com, a small business resource for reaching local customers with social media and mobile marketing. When he's not creating innovative solutions that accelerate business processes, Tim likes to play the mandolin and craft furniture.

Related Articles

Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.