If you are receiving a 403 Forbidden error on a page when trying to access a page with a space URL encoded as %20, follow along for the fix.
Problem
Accessing example.com/index.php?query=two words will resolve to a URL encoded version of example.com/index.php?query=two%20words . This page results in a 403 Forbidden error.
Diagnostics
Accessing example.com/index.php?query=two+words works properly.
$ wget https://www.example.com/index.php?query=two%20words--2024-06-27 16:27:21-- https://www.example.com/index.php?query=two%20wordsResolving www.example.com (www.example.com)… 1234:f1c1:123f:f987::1f1, 12.123.234.123Connecting to www.example.com (www.example.com)|1234:f1c1:123f:f987::1f1|:443… connected.HTTP request sent, awaiting response… 403 Forbidden2024-06-27 16:27:21 ERROR 403: Forbidden.
Solution
A likely cause is your .htaccess file.
Open the .htaccess file in your website’s home/root/public_html directory using your favorite text editor, and find the line that looks similar to:
RewriteRule (.*)$ /index.php?path=$1 [NC,QSA,L]
Edit this line to add the B
flag, which instructs RewriteRule to escape non-alphanumeric characters:
RewriteRule (.*)$ /index.php?path=$1 [B,NC,QSA,L]
Save your .htaccess file, and try reloading your page with URL encoded spaces ( %20 ) again.