使用.htaccess无法正常删除尾部斜杠

My File structure is:

/test/
    myfile.php
    .htaccess

The codes inside .htaccess file are:

RewriteEngine On
RewriteBase /test/

RewriteRule (.*)/ $1 [QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) myfile.php?q=$1  [QSA]

And inside myfile.php the code is:

<?php
     var_dump($_GET['q']);
?>

So when I'm browsing http://127.0.1.1/test/aaa/bbb/ccc I get this output in the browser:

string(15) "aaa/bbb/bbb/ccc", which is perfect.

But when I'm trying to browse http://127.0.1.1/test/aaa/bbb/ccc/ I got this output which is not expected:

string(20) "aaa/bbb/ccc/bbb/ccc/"

Can anyone please explain why this happening?? I have also tested this on webserver.

The default behavior for the * operator is to be greedy (it'll match as many characters as possible, which includes any trailing slash). Try adding a ? modifier to switch to non-greedy mode:

RewriteRule (.*?)/ $1 [QSA]