Search code examples
regexapache.htaccessmod-rewriteescaping

Percentage sign inside .htaccess mod_rewrite


My server keeps throwing a 404 error when i include a percentage sign(%) inside my mod_rewrite regex. I have searched everywhere with no avail. I am running apache 2.4.4 and have tried the B flag shown below.

RewriteRule ^pages/([a-zA-Z]+)/([a-zA-Z+%'"]+)$ pages/$1.php?search=$2 [B]

So when i access pages/animals/cats%20and%20dogs it fails. But works like a charm when i change ([a-zA-Z+%'"]+) to (.*)

Regex level = toddler, so any help is greatly appreciated.


Solution

  • %20 is matched using \s (space) in mod_rewrite. However based on comments below it appears you want to match other non-word characters as well hence it is better to use a \W

    So change your rule to:

    RewriteRule ^(pages)/([a-z]+)/([a-z\W]+)$ $1/$2.php?search=$3 [B,L,NC]