Hello everybody.

I want a super flexible .htaccess rewrite that allows me to change

http://test.com/news.php?p=1

to

http://www.test.com/news/1

I have code that currently does that, I just needed to show what I am striving for. But it has a few problems in it that I can't seem to fix.

This is my current code

Code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.com
RewriteRule (.*) http://www.test.com/$1 [R=301,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ $1.php?p=$2 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
This is the line I think that is causing the problems.
Code:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ $1.php?p=$2 [L]
The issues are:
There is an internal server error when there is junk in the second parameter. Like /news/~. Is there a way to prevent this from showing? I tried doing (.*) to allow everything, but it still shows.

I want there to be multiple query strings, however many the redirect passes to it. So like
/news/3/6/2

Those could mean anything, but if they are provided, I need them to be passed on in the rewrite for the code to pick up.

If there is a real directory, like /dev/, then if you do /dev/3 the code picks it up and shows a 404 error and says dev.php can't be found. How can I make it unaffect real directories?

Also, I have another question, when I make the first slash optional (/?), it truncates the last character of the file name. Why does this happen? As a result, I had to use htaccess code that removed trailing URLs, since it couldn't be optional.

Thanks very much for any help!

NBS