skip to Main Content

I have htaccess rewrite rule that allows account nickname to be like domain.com/its_super-nick.name3000

Here is the regular expression I use, but it gives me an Internal Error back

RewriteRule ^([A-Za-z0-9-_.]+)/?$ index.php?cl=account&username=$1

Am I doing something wrong?

2

Answers


  1. Perhaps it’s because of 9-_ as you probably wanted to match "A" to "Z", "a" to "z", "0" to "9" and then the chars "-", "_" and ".".

    In this case you have to put the hyphen at the end:

    RewriteRule ^([A-Za-z0-9_.-]+)/?$ index.php?cl=account&username=$1
    
    Login or Signup to reply.
  2. RewriteRule ^([A-Za-z0-9-_.]+)/?$ index.php?cl=account&username=$1
    

    The "problem" here is that the regex also matches index.php (the rewritten URL). So, username=index.php will result from a second pass through the rewrite engine. (By itself, this shouldn’t cause an "Internal Error" – if by that you mean a "500 Internal Server Error" – but in combination with other directives this could result in a rewrite loop, ie. a 500 Error)

    And you are missing the L (last) flag.

    The placement of the hyphen towards the end of the character class (as mentioned in the other answer) isn’t actually an issue in this case (it will match a literal hyphen), however, you should move it to the start or end of the character class to avoid any ambiguity (and improve readability).

    Try the following instead:

    RewriteRule ^(?!index.php$)([w.-]+)/?$ index.php?cl=account&username=$1 [L]
    

    This uses a negative lookahead to avoid matching index.php.

    The shorthand character class w is the same as [A-Za-z0-9_].

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search