This kind of htaccess code is a little complex for me as I'm just beginning to understand redirects with htaccess. Could someone help me with this? Thanks in advanced.


Using mod_rewrite in .htaccess it's relatively straightforward to redirect to the "current year" if that year exists as a physical directory on the filesystem.

For example, you can do something like the following at the top of the .htaccess file in the document root:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/%{TIME_YEAR} -d
RewriteRule ^$ /%{TIME_YEAR}/ [R=302,L]

You don't need to repeat the RewriteEngine directive if already present in the file.

Note that this a temporary (302) redirect, so that the redirect is picked up when the "current year" changes.

Problem... redirecting to a "previous year" is not so trivial as you can't perform basic arithmetic in .htaccess alone to say subtract 1 year etc. For this you would need server-side access to create a RewriteMap that calls an external function. Or rewrite to a (PHP) script that is able to determine the correct year to redirect to.

However, this is presumably related to your other question on the WordPress stack. In which case is there a physical directory for years 2013 onwards?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.