Home > Blockchain >  How to add (plus) to regex in Apache RewriteRule?
How to add (plus) to regex in Apache RewriteRule?

Time:11-28

I currently have the following in my .htaccess file, which rewrites A-Z, 1-9,a-z, and - inputs to a PHP file:

RewriteRule ^([A-Z­a-z­0-9­-] )/?$ index.php?url=$1 [L]

I need to add do the regex. How can I do this?

I've tried:

RewriteRule ^([A-Z­a-z­0-9­- ] )/?$ index.php?url=$1 [L]

I've also looked at several regex cheat sheets, which really are not useful.

CodePudding user response:

Did you tried to use Unicode Hex Character Code ?

\x2b

CodePudding user response:

At face value the regex you've posted is "OK" and should work (although you should consider backslash-escaping the last hyphen in the character class, or moving this to the last character in the character class). The can be used unescaped in the character class as it carries no special meaning here.

For example:

RewriteRule ^([A-Za-z0-9 -] )/?$ index.php?url=$1 [L]

HOWEVER, you have a soft-hyphen (U 00AD) - invisible to the naked eye - embedded between each range in the character class, which could be breaking the regex. For example, expanding the hidden unicode character, this is the actual (invalid) regex you are trying to use:

^([A-ZU 00ADa-zU 00AD0-9U 00AD- ] )/?$
  • Related