Search code examples
phphtmlregex.htaccessutf-8

rewrite for .htaccess not picking up numbers in the regex


I have written a rewrite into the .htaccess file so that it replaces any white spaces in the URL with a . (dot).

It works fine apart from when there are numbers in the URL.

RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1.$2 [N,E=NOSPACE:1,DPI]

any help would be much appreciated

  • /search/test test = works with the above regex
  • /search/ 123 123 = does not work

Solution

  • RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1.$2 [N,E=NOSPACE:1,DPI]
    

    The characters %, 2 and 0 in the regex character class are seen as 3 literal characters, not a single URL encoded space (ie. %20). So any numbers containing 2 or 0 that also contain or are followed by a space are going to fail since the regex will fail to match. The RewriteRule pattern matches the %-decoded URL-path, so attempting to match %20 is not required anyway.

    So, you basically just need to remove %20 from your rule. For example:

    RewriteRule ^([^\s]*)(?:\s)+(.*)$ $1.$2 [N,E=NOSPACE:1,DPI]