Ryan Castle Ryan Castle - 11 months ago 31
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

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
, the page redirects just fine, and the PHP handles the lack of the query string. However, when the server receives the request of
, it returns a 404 error.

How can I prevent this from happening?


Redirect to the correct URL by prepending a rule

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

or append /? to your regexp.