Ryan Castle Ryan Castle - 6 months ago 8
PHP Question

.HTACCESS - Prevention of 404 with no URL query

I'm using .htaccess to re-write some of my URLs for my web application. The following is used to make the

user
page a little prettier when user_urls (practically a URL friendly username) are passed to it:

RewriteRule ^user/([0-9a-zA-Z-]+) /user?url=$1 [NC,QSA]


When I input the URL
http://example.com/user
, the page redirects just fine, and the PHP handles the lack of the query string. However, when the server receives the request of
http://example.com/user/
, it returns a 404 error.

How can I prevent this from happening?
Thanks.

Answer

Redirect to the correct URL by prepending a rule

RewriteRule ^(.*)/$ /$1 [L,R=301]

or append /? to your regexp.