Forum Moderators: phranque

Message Too Old, No Replies

Add dates to URL with mod rewrite

like: http://www.example.com/foo/bar/2007/1

         

bedlam

8:56 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hiya,

I'm using a popular CMS that rewrites GET parameters in the URL the usual way; e.g. it turns

http://example.com/index.php?q=foo/bar

...into

http://example.com/foo/bar

So far so good. But now, I need to try to append the current year and month on to the url, and this is where the headaches start.

Judging from the available variables in Apache (listed under RewriteCond [httpd.apache.org], but also available to RewriteRule [httpd.apache.org]), this should be possible by appending %{TIME_YEAR}/%{TIME_MON} to the rewritten url.

The base RewriteRule/Cond set used by the CMS looks like this:

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Reading the Apache docs tells me that this means:

  1. Provided the requested file is not a file and not a directory,
  2. Pass the entire requested filename to index.php,
  3. Stop rewriting here (the 'L' parameter),
  4. Append any existing query string (I think; in any case, this is the 'QSA' parameter).

My best guess was that I should insert a new RewriteCond after 1 and rewrite the incoming url before 2. I thought this could be accomplished by adding

  • RewriteCond %{REQUEST_FILENAME} foo\/bar$ #If the requested filename ends with foo/bar...
  • RewriteRule ^(.*)$ index.php?q=$1/%{TIME_YEAR}\/%{TIME_MON} #Add the current month and the current year to the url...

I thought that this should work and that step 2 could just take care of things normally, but in fact the new condition and rule have no effect whatsoever.

Can anyone point out any obvious errors with my approach or suggest an alternative?

-b

ahmedtheking

9:04 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you need to work backwards:

Where you said:

RewriteRule ^(.*)$ index.php?q=$1/%{TIME_YEAR}\/%{TIME_MON}

It shoudl really be

RewriteRule ^((a-z)/(a-z))/(0-9){4}/(0-9){2}$ index.php?q=$1

Something like that. That'll match, I hope, foo.com/foo/bar/2007/01 to index.php?q=foo/bar

bedlam

9:14 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you need to work backwards:

Thanks, but I think I must have explained it backwards ;-)

I (somewhat bizarrely) need the requested url 'http://example.com/foo/bar' to end up at 'http://example.com/foo/bar/2007/1' (via http://www.example.com/index.php?q=foo/bar/2007/1).

Your example, as far as I can tell sends the latter to the former, but everything already works in this direction.

-b

jdMorgan

10:25 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to append the date to the links by modifying your script, and then have mod_rewrite pass that date info through to your script, if I understand your question.

The "real URL" is determined by what your script outputs on your pages, while the "real filepath" is determined by how you rewrite those URLs, when requested from your server, to the form needed to re-invoke your page-generation script to produce the next page.

The entire process, from a click on a link on your page, through the URL rewriting, to the generation of the next page, is explained here [webmasterworld.com].

Jim

[edited by: jdMorgan at 10:27 pm (utc) on Jan. 17, 2007]

bedlam

10:40 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aha!

Thanks JD--it looks like I got hung up on the very first point in your article: Mod_rewrite cannot "change" the URLs on your pages. If what I'm trying to do is not possible with mod_rewrite, then I guess that explains my lack of success... ;)

-b