Forum Moderators: phranque

Message Too Old, No Replies

substract 1 to current year in an htaccess file

question about %{TIME_YEAR}

         

pixeline

9:41 am on Jan 24, 2008 (gmt 0)

10+ Year Member



hi!

in my .htaccess file i assign a default value to a query string variable related to the year:


RewriteRule ^projects/([a-z0-9-]+)/$ metalab2.php?section=projects&sortBy=time&dsAnchor=%{TIME_YEAR}&item=$1 [NC,L]

question: i need to have TIME_YEAR-1, i've tried many ways but they all fail. what's the correct method of getting the calculation to be performed?

thanks for your help!

Alex

jdMorgan

3:02 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only way to do a true subtraction in .htaccess is to declare a RewriteMap in httpd.conf and call it from within .htaccess to run a server-side script to calculate the needed value. This *does* require that you have access to httpd.conf, though, to declare the RewriteMap, and you must write the script (e.g. PERL) for the RewriteMap to invoke.

If you have a limited number of years you must handle, you can do a lookup-table-based string substitution rather than numerical subtraction:


RewriteCond %{TIME_YEAR}<>1996 ^1997<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>1997 ^1998<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>1998 ^1999<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>1999 ^2000<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2000 ^2001<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2001 ^2002<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2002 ^2003<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2003 ^2004<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2004 ^2005<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2005 ^2006<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2006 ^2007<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2007 ^2008<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2008 ^2009<>(.+)$ [OR]
RewriteCond %{TIME_YEAR}<>2009 ^2010<>(.+)$
RewriteRule ^projects/([a-z0-9\-]+)/$ metalab2.php?section=projects&sortBy=time&dsAnchor=[b]%1[/b]&item=$1 [NC,L]

Obviously, this won't work well (efficiently) for more than a small range of years, and the list must be maintained over time. The rule will not be invoked for years falling outside the ranges covered by the RewriteCond patterns.

Note that "<>" has no meaning whatsoever to mod_rewrite: It is used only as a 'boundary marker' between the concatenated RewriteCond strings. You can leave it out if you prefer, or use any other "harmless and rare" character or character sequence. I chose "<>" simply to imply concatenation.

The first RewriteCond basically says, "If the current year string is 1997, put the string value 1996 into the %1 variable."

Jim