1

I am trying to create some rewrite rules to organize content by date via URL in a short, readable way.

i.e.

http://mysite.com/post/index.php?a=Dave&b=2012&c=12&d=31&e=1
http://mysite.com/post/index.php?a=Dave&b=2012&c=12&d=31
http://mysite.com/post/index.php?a=Dave&b=2012&c=12
http://mysite.com/post/index.php?a=Dave&b=2012
http://mysite.com/post/index.php?a=Dave&b=all

becomes

http://mysite.com/post/Dave/2012/12/31/1
http://mysite.com/post/Dave/2012/12/31
http://mysite.com/post/Dave/2012/12
http://mysite.com/post/Dave/2012
http://mysite.com/post/Dave/all

This is the .htaccess I have in //mysite.com/post/

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)/?([0-9]+)/?([0-9]+)/?([0-9]+)?$ index.php?a=$1&b=$2&c=$3&d=$4&e=$5 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)/?([0-9]+)/?([0-9]+)/?([0-9]+)/?$ index.php?a=$1&b=$2&c=$3&d=$4&e=$5 [L,NC,QSA]

RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)/?([0-9]+)/?([0-9]+)?$ index.php?a=$1&b=$2&c=$3&d=$4 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)/?([0-9]+)/?([0-9]+)/?$ index.php?a=$1&b=$2&c=$3&d=$4 [L,NC,QSA]

RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)/?([0-9]+)?$ index.php?a=$1&b=$2&c=$3 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)/?([0-9]+)/?$ index.php?a=$1&b=$2&c=$3 [L,NC,QSA]

RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)?$ index.php?a=$1&b=$2 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/?([a-zA-Z0-9]+)/?$ index.php?a=$1&b=$2 [L,NC,QSA]

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?a=$1 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?a=$1 [L,NC,QSA]

Now the problem is some of the URLs don't work. From the above examples, three of them work as expected. but the others output in these ways:

URL: http://mysite.com/post/Dave/2012/12
OUTPUTS:
    a => Dave
    b => 2012
    c => 1
    d => 2
URL: http://mysite.com/post/Dave/2012
OUTPUTS:
    a => Dave
    b => 20
    c => 1
    d => 2

And this is obviously not the correct values, apparently splitting up the number across the other variables.

Why does it do this and what must I do to fix it?

Felixpitau
  • 21
  • 8

1 Answers1

2

Don't say /? everywhere. The ? means that the preceding expression is optional. This means that 2012 matches ([0-9]+)/?([0-9]+)/?([0-9]+)/?, because those slashes don't have to be there. What you want is ([0-9]+)/([0-9]+)/([0-9]+)/?, because you want that the slash at the end of the URL is optional.

  • So that must also mean I don't need to double all the rewrite rules for a leading slash? The solution works wonderfully and has fixed other similar issues I've had, thanks! – Felixpitau Dec 31 '12 at 19:14
  • A leading slash would be a slash at the beginning, so I assume you mean a trailing slash. Yes, that's exactly what you would use `?` for. It seems you were using them in all the wrong places, and none of the right ones ;) –  Dec 31 '12 at 19:17
  • Yes, trailing slash. I found a page where they were using it like that and I had thought it was something to do with where the variables and patterns match up, which in retrospect seems pretty ridiculous. – Felixpitau Dec 31 '12 at 19:20