Creating a default variable in mod_rewrite

I have mod_rewrite take this url:

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

      

and to make it pretty:

mydomain.com/directory/slug/2009/

      

Easy, but now my problem is that if someone lands on a URL with no year attached (ex: 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


source to share


1 answer


Try using server variables:

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

      

If I'm not mistaken this should redirect the url:

mydomain.com/directory/slug/

      

in



mydomain.com/directory/slug/2009/

      

(another 2 and a half months;))

I think you now need to do something if you don't want to redirect or /

at the end of the URL :)

I tried it in a similar case and it worked fine.

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

+2


source







All Articles