开发者

Symfony: how to find out if a request is going to be 404 before it gets there?

My site is multilingual and if a user requests a page without specifying the two-letter language code, he'll be redirected to the proper language (using IP or previous cookie settings). For exam开发者_运维技巧ple you can try going to http://colnect.com and see you'll be redirected to colnect.com/en or colnect.com/es or other (53 languages are supported).

The PROBLEM is when I get bogus requests (mostly from bots) such as colnect.com/fdlghsweru my redirection code first redirects it (code 302) to colnect.com/en/fdlghsweru which will then return 404. This means: * There are two requests instead of one -> more server load * Some bots will either try colnect.com/fdlghsweru again later or even believe it exists

The code I've done is currently located in MyUser::setCulture()

Is there a good place where I can locate the code only when I know an action is about to get executed and not get 404?

UPDATE: what I currently do is check in my code if $sfRequest->getParameter('module') is one of the valid modules in my application. This covers most bogus requests but not the one created by malfunctioning bots which append bogus parameters to existing URLs.

Thanks


myUser represents a session. It's not the best place to handle redirections.

You could listen to the *user.change_culture* event and handle your redirection in an action but it's still not a straight forward way.

Another solution would be implementing a filter. You'd be able to recognize wrong URLs quite early in the execution process.

It a little bit depends on how many routes you want to redirect. If it's only one route than you could directly check if the target page exists and forward to 404 otherwise.

On the other hand, if you're redirecting everything without culture (why? old routes?) than I guess the best way would be to make a redirection first and then let the target page handle forwards to 404 if needed. Pretty the same as currently but not withing the myUser class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜