Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 704
Ricky Boyce Avatar asked Dec 06 '25 13:12

Ricky Boyce


1 Answers

%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]
like image 70
anubhava Avatar answered Dec 08 '25 05:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!