spartan
spartan

Reputation: 1

How to do a basic URL rewrite with mod_rewrite

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?

Upvotes: 0

Views: 161

Answers (1)

anubhava
anubhava

Reputation: 785246

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]

Upvotes: 1

Related Questions