fixes #4520 Vanity URLs redirect to a directory name fail

Fixed the issue and pushed the code for review, please check it.