如何使用mod_rewrite进行基本的URL重写

I don't understand why but when I use this:

#RewriteRule ^/?r/(.*)$ /index.php?n=$1 [L]

to rewrite mysite.com/r/somewhat to mysite.com/index.php?r=somewhat the site work.

But if I use this:

#RewriteRule ^/?(.*)$ /index.php?name=$1 [L]

to rewrite mysite.com/somewhat to mysite.com/index.php?r=somewhat, my site stop working.

I don't understand why. Someone can help me?

How can I rewrite mysite.com/somewhat to mysite.com/index.php?r=somewhat?

Your 2nd rule will cause infinite looping since target URI /index.php?r=somewhat also matches .*. Eventually it causes 500 internal server error.

To fix you need to avoid rewriting files and directories using RewriteCond like this:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /index.php?r=$1 [L,QSA]