Q:

htaccess - Category / Subcategory / Page

I have a small problem:
My URL: localhost / cat / Category / Subcategory / Page
Example: localhost / cat / movies / 3d / 7
The Code:
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2 [NC,L]  ## wq is Category
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]  ## wsq is Subcategory / but if no Subcategory and Pagination -> False it must be "&wpq="
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3&wpq=$4 [NC,L]   ## wpq is Pagination

My current htaccess:
RewriteEngine On

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

RewriteRule ^([a-z0-9\-]+)/?$ $1.php [NC,L]

RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2 [NC,L]  ## wq is Category
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]  ## wsq is Subcategory / but if no Subcategory and Pagination -> False it must be "&wpq="
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3&wpq=$4 [NC,L]   ## wpq is Pagination

RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w.-]+)/?$ $1.php [L]

This works as far as it should, only problem if only one category (and no subcat) is entered, then it should be checked whether parameter 2 only numbers.
If only numbers are entered, then it is not a subcategory but the Pagination parameter (then the second parameter must be "wsq" and not "wsq").
I hope you understand what I mean: D
This could be done with Php, but I think via .htaccess is better?
Thank you in advance already times.

A:

If I get it, replace:
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]

With:
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/(\d+)/?$ $1.php?wq=$2&wpq=$3 [NC,L]
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]

(\d+) (which is equivalent to ([0-9]+) or ([[:digit:]]+)) to match a number. This new rule being more specific, needs to be located before your "regular" rule (which expects letters and/or digits and/or dash).

