agustin
agustin

Reputation: 2387

URL redirect with htaccess regex

I'm trying to redirect from r.php?username={variable} to r/{variable}/ (last slash optional) with my htaccess.

Username:

  1. Must start with a letter.
  2. Must finish with a letter or number.
  3. Can contain underscores.

So, $username is correct if preg_match('/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/',$username)

So, now, what's wrong with my htaccess?

RewriteRule ^r/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/?$ r.php?username=$1

Upvotes: 1

Views: 60

Answers (1)

Croises
Croises

Reputation: 18671

Try with this .htaccess:

RewriteRule ^r/([A-Z](?:[A-Z0-9_]*[A-Z0-9]+)?)/?$ r.php?username=$1 [NC,L]

Upvotes: 1

Related Questions