Emanuel Vintilă
Emanuel Vintilă

Reputation: 1939

Mod rewrite only numeric query strings

When a user visits /user he will see his own profile, but when he visits /user/[id] he will see the user's profile with the specified id. However, somebody could also type /user/[random string] which leads into a 500 error on my server. So I want to redirect any non-numeric request to a 404 page.

Here are my RewriteRules

RewriteEngine On
RewriteBase /

RewriteRule ^user/edit /edit.php
# RewriteRule ^errors/404.html /user.php?id=$([\w])
RewriteRule ^user/([\d])$ /user.php?id=$1

If the second one is commented or not, it doesn't matter, any non-numeric query string will produce a 500 error.

Upvotes: 0

Views: 151

Answers (1)

Ivan Cachicatari
Ivan Cachicatari

Reputation: 4284

Modify your rule adding a plus:

RewriteRule ^user/([\d]+)$ /user.php?id=$1 [L]
RewriteRule ^user/.+$ /errors/500.html [L]

or:

RewriteRule ^user/([0-9]+)$ /user.php?id=$1 [L]
RewriteRule ^user/.+$ /errors/500.html [L]

The [L] modifier stops parsing, is the reason what error 500 rule must place after first rule.

Upvotes: 1

Related Questions