views:

30

answers:

1

I have mod_rewrite take this url:

mydomain.com/directory/index.php?slug=slug&year=2009

and make it pretty:

mydomain.com/directory/slug/2009/

Easy, but now my issue is, if someone lands at the URL without the year attached (like: mydomain.com/directory/slug/), how can I add the current year to the URL?

My current htaccess reads:

RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)?$ /directory/$1/$2/ [R]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /directory/index.php?slug=$1&year=$2 [L]
+2  A: 

Try using server variables:

RewriteRule ^([^/\.]+)/?$ /directory/$1/%{TIME_YEAR}/ [R]

If I didn't make a mistake this should redirect url:

mydomain.com/directory/slug/

to:

mydomain.com/directory/slug/2009/

(for another 2 and half months ;) )

I think you now what to do, if you don't want redirect or / at the end of url :)

I tried this in similar case, and this worked fine.

Oh, and you can find list of server variables in mod_rewrite documentation. I am not sure if this will always work.

prostynick