ErrorDocument 403 /index.php?a=http403
ErrorDocument 404 /index.php?a=http404

RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ index.php?a=$1&b=$2&c=$3&d=$4 [QSA,NE,L]
RewriteRule ^(.*)/(.*)/(.*)/$ index.php?a=$1&b=$2&c=$3 [QSA,NE,L]
RewriteRule ^(.*)/(.*)/$ index.php?a=$1&b=$2 [QSA,NE,L]
RewriteRule ^(.*)/$ index.php?a=$1 [QSA,NE,L]

RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [QSA,R=301]