正则表达式URL打破某些字符

My regex works when doing things like foo/bar and one with spaces/two with spaces/three with spaces/four with spaces etc, but it directs to the 404 page when using urlencode in PHP.

The following test shows that encoded strings don't work in the URL: http://regex101.com/r/jP0gW1

Anyone have any ideas? It also breaks when using the "+" character.

That's because you were not handling the % in your regex. \w only matches alphanumerical characters.

Adding a % to your regex made it work:

^([\w\ ]+)(?:/([\w\ ]+))?(?:/([\w\% ]+))?(?:/([\w\ ]+))?(?:/([\w\ ]+))?(?:/([\w\ ]+))?/?$

DEMO

You can simplify your regex if you want to:

/^([\w\s%]+\/).*$/