skip to Main Content

I have a link like this
www.example.com/profile.php?name=sagar123

I used this rule:

RewriteRule ^profile/([a-zA-Z0-9_-]+)$ profile.php?name=$1 [L]

and now I can chang my URL to like this:

www.example.com/profile/sagar123

everything is fine but, now I want to use Hindi language characters also like this

www.example.com/profile.php?name=सागर (It’s working fine)

www.example.com/profile/सागर (It is not working and showing Server error)

Please help me to write a rule or regex to accept all ([a-zA-Z0-9_-]+) and also Hindi Character.

Thanks and regards,

2

Answers


  1. Use the (.*) regex class to match any type of character.

    Also, you don’t need the + operator at the end in your capturing ( and ) parens, as you’re using ^ to indicate the beginning of the URL line, and $ to indicate its end, so a + greedy operator doesn’t get you anything extra.

    It should look like…

    RewriteRule ^profile/(.*)$ profile.php?name=$1 [L]
    

    If you need further info, I recommend taking a look at Apache.org: Apache mod_rewrite Introduction. They cover most of the characters I’ve discussed in this post up to this point: ., (, ), +, etc..

    Login or Signup to reply.
  2. Hindi chars falls between u0900-u097F range. So you can use this inside character class.

    To answer your question, most regexes(PCRE) do not support u notation and support format of x{900}

    ([x{900}-x{97F}a-zA-Z0-9_-]+)$
    

    In python u is supported, so :

    ([u0900-u097Fa-zA-Z0-9_-]+)$
    

    see this for regex matching demonstrating both English and Hindi chars getting matched.

    Also, see this for reading literal hindi char mapped to their hex values.

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