Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top