Closed planetahuevo closed 9 years ago
I think that the plugin could help in such situations but I haven't decided how yet. Here's a proposal I've made before.
As of now, here's what you can do:
$f3->set('ONERROR',function($f3){
$ml=Multilang::instance();
if ($f3->ERROR['code']==404 && array_key_exists($new='/en'.$f3->PATH,$f3->ROUTES))
$f3->reroute($new,TRUE);
return FALSE;
});
This will catch 404 errors and perform a 301 redirection if a corresponding english route exists.
I'll come up later with something at the plugin level.
Thanks! Do you prefer to keep the conversation here or at the googlegroup list?
That suggestion on the link sounds good. But what we need is something in between both.
On your example, I would choose OFF, but in that case I need that all the "non language urls" are redirected to the default language urls. The perfect solution is that the redirection works by default as it works on the root url. So, when no language is present, just redirect and load the default url language.
Hi @planetahuevo, I prefer to keep the conversation here as it is directly related to the plugin.
Indeed there are many possible scenarios:
/contact/
=> /en/contact
+ /es/contact
(/contact
= 404)/contact/
=> /en/contact
+ /es/contact
(/contact
= 301 to /en/contact
)/contact/
=> /contact
+ /es/contact
(/en/contact
= 404)I deliberately restricted the plugin behavior to the first scenario, to keep the configuration options fat free but I'll have to change that. The first scenario fits well a fresh multilingual project, but not the localization of an existing monolingual app.
I agree. My case is the second scenario here, which is updating the urls for a multiligual project that has not been created properly... We have one url and two languages, which is bad for seo and google do not recommend it. https://support.google.com/webmasters/answer/182192?hl=en&ref_topic=2370587
I cannot see when the third one is needed. If you have a multilanguage international site, no language should be more important than other... but could be a nice to have. now that I am writing this, I realize that maybe the frontend webpage of a project could use the third scenario, but this will work too using the second option.
If you decided to consider the third one you should also add this fourth, which is the same thing:
I do not think 3rd and 4th are really needed, but that is my opinion I am sure others will disagree. :)
Hi, will be possible to create a reroute with regex to old url using the rerouting options on fat free and add that after the multilang instace to make the redirection work?
Yes, and it works, but I have problems when the user type the route with a / at the end or not. On my routes none of them have the / so when the user write it with but on that one, so as we are checking against my routes.ini file some routes wont work.
But we are working on a solution and we will share it here in a moment. -testing it right now-
Thanks
This is what we are using so far:
$f3->set('ONERROR','myErrorHandler');
function myErrorHandler($f3) { // custom error handler code goes here
if($f3->ERROR['code']==404 && strrpos($f3->PATH,'/es/')===false
&& strrpos($f3->PATH,'/en/')===false && strrpos($f3->PATH,'/api/')===false){
$new='/es'.$f3->PATH;
$f3->reroute($new,TRUE);
}
}
With this we make sure we do not break the api at all and when the route does not have any language we just add the /es to it.
Last thing (which is a fatfree thing, not your plugin) is to capture the whole url, not just the path. We are loosing some variables typed on the url but it works fine so it is a minor thing to fix.
Thanks and I hope this helps others with the same issues :)
Ah ok, I understand the trailing slash issue: a rtrim
was missing in my example:
$f3->set('ONERROR',function($f3){
$ml=Multilang::instance();
if ($f3->ERROR['code']==404 && array_key_exists($new='/en'.rtrim($f3->PATH,'/'),$f3->ROUTES))
$f3->reroute($new,TRUE);
return FALSE;
});
This code shouldn't mess with your api, since it checks for existing english routes:
/en/contact
exists, then /contact
redirects to it/en/foo
doesn't exist, then /foo
throws a 404And now I've understood the 2nd part of your message ;) My code doesn't work with parameterized routes (/contact/@token
)..
All right, really need to find something at the plugin level!
Sorry for my english.:D Not my first language. Thank you for the effort on understanding! :+1:
The code I post before works with the parameterized routes, as it just redirect anything that doesn´t have the /en or /es. It is a nice temporary solution and not hard to update if required.
Trying to find the solution for dynamic urls now.
Yeah no problem, I struggle myself with English everyday ^^
Concerning your last issue:
We are loosing some variables typed on the url
Do you mean the query string? Like /contact?var=hola
redirects to /es/contact
instead of /es/contact?var=hola
?
If yes, you have two possibilities:
$f3->REALM
instead of $f3->PATH
$f3->reroute($new.'?'.$f3->QUERY,TRUE)
instead of $f3->reroute($new,TRUE)
That is the problem. :+1: I will try both suggestions and see how it works.
Thanks!
Hi @planetahuevo, I've added a migration mode to the plugin, which automatically redirects old URIs to the new primary language.
Hope it helps!
Hi, Thank you! Been busy those days. We will try to test it soon but I am sure it will work as expected.
Hi, I have test the migration mode and it works well when the url are static but when we have a variable /@url/ on the url it just redirect to the @url without capturing the value that was there. I will keep using the 404 error to redirect for now.
Let me know if I can help you to understand the problem.
Hi @xfra35 That is great. We will keep using our code until the next release of Fatfree 3.4.1 and we will update everything at the same time.
Thank you for the info!
Hi, I am addind this plugin to an existing fatfree installation. How can I redirect the old urls to the default language?
Example
domain.com/old/route/app should be redirected to domain.com/en/old/route/app
but this should not be redirected because it works by default:
domain.com/es/old/route/app
Thanks