Our URLs with a URL encoded trailing white space (%20
) are producing a 404 error. The application is run on Codeigniter on Apache.
-
/directory/page%20
will return a 404 error -
/directory/page
will return a 200 OK
How can I route all URLs with a trailing %20
to the intended URL?
2
Answers
@Juan Cullen, this is a common issue when you have a space before a trailing slash. For example lets say: “http://example.com/directory/page /”. you can notice the space before the trailing slash.
To solve this for all urls that have such behavior, you can use PHP’s rtrim() function.
Check the code below
Now you can call it like this:
As you are using Codeigniter, you can put this function in a helper file and access wherever you want.
This is an Idea try it out and let me know if it works.
In that case you can add something like the following at the top of your
.htaccess
file to redirect (canonicalise) such requests to remove the trailing space.For example, before the Codeigniter front-controller:
The “processed” URL-path matched by the
RewriteRule
pattern has already had the trailing slash removed, however, theREQUEST_URI
server variable has not. So, we can check for the trailing space on theREQUEST_URI
and simply redirect to “the same” (processed) URL-path, as captured by theRewriteRule
pattern.The
REQUEST_URI
server variable is already %-decoded. Thes
shorthand character class matches against any whitespace character and the trailing$
anchors this to the end of the URL-path.Test first with a 302 (temporary) redirect to make sure that it works OK before changing to a 301 (permanent) redirect.