Scheduled redirects are a powerful feature for web administrators, allowing you to automatically redirect visitors based on specific dates and times. This is incredibly useful for managing maintenance windows, seasonal promotions, or time-sensitive content without needing complex server-side scripts like PHP.
The Apache web server, through its .htaccess configuration files, provides a straightforward way to implement these time-based redirects using server variables.
Understanding Time Variables in .htaccess
Apache's mod_rewrite module allows you to access various date and time values through the %{TIME_XXXX} syntax. These variables represent different components of the current server time:
%{TIME_YEAR}: The current four-digit year (e.g., 2025)%{TIME_MON}: The current month (01-12)%{TIME_DAY}: The current day of the month (01-31)%{TIME_HOUR}: The current hour using a 24-hour clock (00-23)%{TIME_MIN}: The current minute of the hour (00-59)%{TIME_SEC}: The current second of the minute (00-59)%{TIME_WDAY}: The current day of the week (0-6, where 0 is Sunday)%{TIME}: A complete formatted string representing the date and time down to seconds (e.g., 20250723130645)
Implementing Scheduled Redirects
To use these variables, you'll typically combine them with RewriteCond (Rewrite Condition) and RewriteRule directives in your .htaccess file. Ensure that mod_rewrite is enabled on your server for these directives to function.
Example 1: Redirecting to a Date-Specific URL
You can dynamically build a redirect URL that includes the current date. For instance, to redirect /articles/today to a URL like /articles/2025-07-23:
RewriteEngine On
RewriteRule ^articles/today$ /articles/%{TIME_YEAR}-%{TIME_MON}-%{TIME_DAY} [R=301,L]
Example 2: Redirecting After a Specific Date and Time
To redirect a page once a particular date and time has passed, you can compare the current time to a target time. This is often done by concatenating the time variables into an integer for easy comparison. The following example redirects the root URL (/) to /maintenance.html if the current time is after 9:00 AM on July 24th, 2025:
RewriteEngine On
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR}%{TIME_MIN} >202507240900
RewriteRule ^$ /maintenance.html [R=302,L]
Note: We use R=302 for temporary redirects (e.g., maintenance) and R=301 for permanent redirects.
Example 3: Redirecting Until a Specific Date and Time
Conversely, you might want a redirect to be active only until a certain time. This example redirects the root URL to /old-homepage.html if the current time is before 10:30 AM on July 25th, 2025:
RewriteEngine On
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR}%{TIME_MIN} <202507251030
RewriteRule ^$ /old-homepage.html [R=302,L]
Example 4: Redirecting Between Two Specific Dates
For scenarios like seasonal promotions, you can set up a redirect that is active only within a specific date range. This requires two RewriteCond directives. This example redirects /promo to /summer-sale.html only between July 1st, 2025, and August 31st, 2025:
RewriteEngine On
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} >20250630
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} <20250901
RewriteRule ^promo$ /summer-sale.html [R=302,L]
Remember that the comparison values (e.g., 20250630) should be constructed to ensure the desired date range is correctly captured. For "greater than" (>), you'd use the day *before* your start date, and for "less than" (<), you'd use the day *after* your end date.
Important Considerations
- Server Time: Redirects are based on your web server's time, not the visitor's local time. Ensure your server's clock is accurate.
- Caching: Be mindful of browser and server caching. A
301 (Permanent)redirect can be heavily cached by browsers, making it difficult to revert. Use302 (Temporary)for time-sensitive or temporary redirects. - Order of Rules: The order of rules in your
.htaccessfile matters. Rules are processed sequentially. - Testing: Always test your
.htaccessrules thoroughly in a development environment before deploying to a live site.
By leveraging these .htaccess time variables, you gain precise control over when and how your website redirects traffic, enhancing your ability to manage content and site availability efficiently.