Time based URL rewriting Apache htaccess


With mod-rewite you can rewite or redirect your URLs based on server date and time logic. You can redirect your visitors to a specific page or URL destination using this date/time logic.
In this post, we will learn how to apply date/time conditions to RewriteRule directive with some code examples of basic to advance Time based URLrewriting.

Let's first have a look at available mod-rewite date/time variables



Mod-rewite date and time variables


The following are the pre-defined apache mod-rewite variables to work with server date and time . The date and time Alpanumbric values these variables contain depend on the timezone set on your server. You can set server timezone using server.config file.





%{TIME} variable is used to match against the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).



%{TIME_YEAR} represents the current year ie 2019. This variables contains year value in YYYY format (4digits).







%{TIME_DAY} variable is used to match against the current Date ie (date of the day from 1 to 31)



%{TIME_MON} is used to match against the current month of the year ie 1 to 12.


%{TIME_HOUR} represents the current hour for example 1 if the time is between 1:00 and 1:59 .



%{TIME_WDAY} week day. This variable is used to match against the current day of the week in numeric format . For example 6 if the day is Saturday.




%{TIME_MIN} is used to match against current minute starting from 00. This variable returns number of minutes from 00 to 59.

%{TIME_SEC} used to match against current secind starting from 00. This variable returns number of seconds from 00 to 59.




To test these variables on your server, create a demo.php in your public_html folder and add the following php code :


<?php
echo $_GET["myvar"];
?>


Next add the following RewriteRule to your htaccess file :


RewriteEngine on
RewriteRule ^test /demo.php?myvar=%{TIME} [L]


Now visit yoursite.com/test you will see the content of demo.php file. Your demo.php file will show you the value of whatever variable was requested. You can manually change the variable in the RewriteRule's destination ( /demo.php?myvar=%{TIME} ).















Time based URL rewriting examples



Suppose you have a website that is updated or maintenanced every friday and you want to redirect your website visitors to specific page ie under-construction.php on this day of week. The rewrite logic you will need for this will be read as :



If the day is Friday
Redirect all visitors to under-construction.php.





here is the RewriteRule:



RewriteEngine on
RewriteCond %{TIME_WDAY} ^5$
RewriteRule (.*) /under-construction.php [L,R]


The rule above redirects all of your website URLs to a single page under-construction.php if the condition is met ie (%{TIME_WDAY} is == 5) . 5 is number of weekday for friday.

This rule above redirects all of your site URLs including the target page url under-construction.php to itself which can cause a Redirect loop error on your server , to fix this error you will need to exclude the target URL from your RewriteRule regex so it can not match the target.



The following is the perfect version of the above rule :



RewriteEngine on
RewriteCond %{TIME_WDAY} ^5$
RewriteCond %{REQUEST_URI} !/under-construction.php [NC]
RewriteRule (.*) /under-construction.php [L,R]




More examples





To redirect your website URLs to a different location at 8PM , you can use the following Rule :



RewriteEngine on
RewriteCond %{TIME_HOUR} ^20$
RewriteCond %{REQUEST_URI} !/location.php [NC]
RewriteRule (.*) /location.php [L,R]


The rule above redirects your website visiters to /location.php if the server time hour is 20 which means 8PM . The rule will be applied from 20:00 to 20:59 .



Redirect all website traffic to example.com at 8:30PM :



RewriteEngine on
RewriteCond %{TIME_HOUR}:%{TIME_MIN} ^20:30$
RewriteRule (.*) http://example.com/ [L,R]


The rule above will redirect your website visitors to example.com exactly at 8:30PM. We used two variables in the RewriteCond test string %{TIME_HOUR} and %{TIME_MIN} separated by a colon : to test the server time using a regex based pattern ^20:30$ .




Hope you liked the post. Thank you for reading!

Post a Comment

0 Comments