How do I get my urls to look like:

www.example.com/test/

instead of www.example.com/test.php

and to make it work in sub directories too like:

www.example.com/test2/file/

instead of www.example.com/test2/file.php