views:

22

answers:

1

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 example 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

A: 

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.

kuba
Thank you for your answer but I'm afraid it doesn't help me. I'm trying to know that an action will be performed and not 404. The reason I'm using myUser is because I need to check if the user's language is set. In such a case, if you're viewing the site in Spanish and get a link from another member who sees it in German, the site would redirect his /de/ link to a /es/ link so you can still see the site in Spanish. It's important for me to keep the /es/ as a part of the URL so if you send the link to a friend, the default (as he's not a user) would be Spanish and not another language.
Collector

related questions