I’m trying out mod_rewrite for the first time and not having much luck so far. My current URL is in the format of http://mywebsite.com/user/index.php?u=123. How can I rewrite the URL to be http://mywebsite.com/user/123?
So far, I’ve (unsuccessfully) tried the following, which resulted in 500 Internal Server Errors
RewriteEngine On
RewriteRule ^user([^/]*)\.php$ /user/index.php?u=$1 [L]
Where am I going wrong? This should be pretty straight forward.
You very nearly had it correct. Rather than
[^/]matching everything up to but not including the next/, you want([\d]+)$matching all digits until the end. You’ll need a/afteruser, and finally, remove the.php.Note, I also removed the leading
/on the right-side rewrite from/user. That may not have been necessary though; I can never remember how it behaves with the leading/unless I test for myself.Most likely, the cause of your 500 error was a rewrite loop. Since you were matching .php on the end, even
user/index.phpwould match the rule, throwing it into an infinite loop.