Forum Moderators: phranque

Message Too Old, No Replies

Problem with htaccess (/year/month/day/)

with two numbers, it works. with 4 numbers, it doesn't

         

guarriman

4:11 pm on May 25, 2007 (gmt 0)

10+ Year Member



I'm trying to make my website works with [domain.com...]
to server [domain.com...]

With Apache 2.2, my .htaccess:
----------------------------
RewriteEngine On
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(.*) /redirect.php [QSA,L]
RewriteRule ^([0-9]+)/([0-9]+) /redirect.php [QSA,L]
----------------------------

If I type:
- [mydomain.com...] it works
- [mydomain.com...] I get a 404 error message.

What am I doing wrong?

Thank you very much.

erichazann

5:58 pm on May 25, 2007 (gmt 0)

10+ Year Member



I copied your code and it works in Apache 1.3.37. So I can't tell you what is wrong. But it's a good idea to use the most restrictive match. Try closing off your match conditions.

RewriteEngine On
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(.*) /redirect.php [L]
RewriteRule ^([0-9]+)/([0-9]+)/?$ /redirect.php [L]

The second rule ensures that both /2007/05/ and /2007/05 work, and then the matching stops.

Your current code lets /2007/05/morestuff/morestuff work[1], and perhaps your rules are conflicting in Apache 2.

If you are sure that the format will always be YYYY/MM/DD then you could further restrict it. Using:
([0-9]{4}) & ([0-9]{2}) in place of the ([0-9]+) (The numbers 4 and 2 denote exact 4 and 2 of the preceding range.)

Also, depending on what "foo" really is, you could further restrict that match from (.*).. especially because your code allows [mydomain.com...] to even work.

Why are you using QSA if you have no query strings appended? Hope that helps you a bit.

[1] In Apache 1.3.37, at least.

jdMorgan

8:02 pm on May 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a note: I see no need to use parentheses in the patterns, since no back-references or alternate match strings are being used.

Jim