This is one of those fixes where it is probably worth me explaining the very specific use case I needed it for first, to serve as an example of exactly why anyone would need this, because on the surface it may seem pointless.
Picture this: Restaurant client. Table cards with QR codes linking directly to menu PDFs, so customers can look at the menu on their phones instead of using a physical print menu that has been handled by dozens of other people.
Note to the future: I’m not sure what the restaurant experience looks like in your world. What I’m describing may be ubiquitous for you now, or may be a complete head-scratcher. Assuming it’s safe for you to touch your head. For context, I am writing this in the midst of the 2020 COVID-19 pandemic.
Here’s the problem: Menus change. URLs referenced by a QR code do not. By default, WordPress automatically creates year and month subfolders inside wp-content/uploads
and puts files in the folder for the year and month the post they’re attached to was created, or if you’re uploading directly into the Media Library, not attached to a post, then the year and month the file was uploaded.
So that means that the URLs embedded in my client’s QR codes contain 2020/09
. But now it’s October, so if they upload a replacement file today, its URL will contain 2020/10
and the QR code will not work. I should note at this point that I do not like the default WordPress behavior of putting files into these subfolders, but I sometimes forget to turn off this setting when I’m creating a new site, or — as is the case here — I’m working on a site someone else originally set up.
My solution: Turn off year/month folders, so that any newly uploaded PDFs with the same filename will have the same URL. (Assuming the client deletes the old one first!)
You may be thinking, well, that’s great, if you had done this before the QR codes were created. Yes, exactly. That’s where this rewrite rule comes in.
When you turn off the year/month folder setting, it doesn’t move any existing files or change any code that links to them. This purely affects new uploads going forward. So what I need is a rewrite rule that will allow existing file URLs with the year/month path to continue working, while automatically removing that bit from the URL and trying to find the same file in the main uploads
folder, if there’s no file at the year/month URL.
OK, here’s the code:
# Redirect file URLs from year/month subfolders to base uploads folder if not found
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp-content/uploads/
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{2})/(.*) /wp-content/uploads/$3 [L,R=301]
</IfModule>
This should go not in your main .htaccess
file, but in an .htaccess
placed inside your wp-content/uploads
folder.
Let’s assess what’s going on here, line by line.
# Redirect file URLs from year/month subfolders to base uploads folder if not found
Just a comment so we remember what this is all about. You may think you’ll remember. But you probably won’t. Comments are your future friend.
<IfModule mod_rewrite.c>
and </IfModule>
Apache configuration conditionals wrapper for all of our actions, to make sure this code doesn’t run if mod_rewrite
isn’t enabled. Honestly I often leave this out because… come on, the entire site is going to be broken if mod_rewrite
isn’t enabled.
RewriteEngine On
If you don’t know what this is about, RTFM. (The “F” is directed at the manual, not you. I hate the Apache documentation.)
RewriteBase /wp-content/uploads/
This is the reference point for the ^
later on. Needs to be the relative path of the uploads folder below your WordPress site’s base URL.
RewriteRule ^index\.php$ - [L]
Honestly we probably don’t need this line, as there shouldn’t be any index.php
files inside your uploads
folder anyway, but it just feels weird not to include it. This just says “don’t do any rewrites to the index.php
file.”
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These lines are very common in this type of rewrite instruction set, and in fact come straight from the default WordPress rewrite rules. They are saying, only apply the following rules to URLs that don’t match existing real files or directories under this path. This is critical to keep all of your existing Media Library URLs working.
RewriteRule ^([0-9]{4})/([0-9]{2})/(.*) /wp-content/uploads/$3 [L,R=301]
This is what we’re here for. Note we are using the magic of regular expressions to make this work. There are three parenthetical groupings, though we technically could eliminate the first two sets of parentheses and change $3
to $1
, but I just like having the parentheses to help me keep things straight.
([0-9]{4})
is matching a 4-digit number, representing the year folder.
([0-9]{2})
is matching a 2-digit number, representing the month folder.
And (.*)
is matching… anything, representing the filename. That’s the bit we want to reference in the replacement string, /wp-content/uploads/$3
which tells Apache to serve up the filename from the year/month URL directly out of the uploads
folder itself.
The final bit, if you’re not familiar with rewrite rules, [L,R=301]
, just says this is the last rule the previous set of conditions applies to, and that it should return an HTTP 301 (permanent redirect) status along with the redirect, which is good SEO karma.