xfra35 / f3-multilang

Create multilingual apps with this localization plugin for the PHP Fat-Free Framework
GNU General Public License v3.0
48 stars 13 forks source link

Redirect old url #1

Closed planetahuevo closed 9 years ago

planetahuevo commented 9 years ago

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

xfra35 commented 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.

xfra35 commented 9 years ago

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.

planetahuevo commented 9 years ago

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.

xfra35 commented 9 years ago

Hi @planetahuevo, I prefer to keep the conversation here as it is directly related to the plugin.

Indeed there are many possible scenarios:

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.

planetahuevo commented 9 years ago

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. :)

planetahuevo commented 9 years ago

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?

xfra35 commented 9 years ago

Hmm I'm not sure to follow you. Did you try the ONERROR trick?

planetahuevo commented 9 years ago

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

planetahuevo commented 9 years ago

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);
 }
}
planetahuevo commented 9 years ago

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 :)

xfra35 commented 9 years ago

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:

xfra35 commented 9 years ago

And 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!

planetahuevo commented 9 years ago

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.

xfra35 commented 9 years ago

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:

planetahuevo commented 9 years ago

That is the problem. :+1: I will try both suggestions and see how it works.

Thanks!

xfra35 commented 9 years ago

Hi @planetahuevo, I've added a migration mode to the plugin, which automatically redirects old URIs to the new primary language.

Hope it helps!

planetahuevo commented 9 years ago

Hi, Thank you! Been busy those days. We will try to test it soon but I am sure it will work as expected.

planetahuevo commented 9 years ago

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.

xfra35 commented 9 years ago

Hey @planetahuevo,

I've added a test for this yesterday and it seems to be working well.

The only thing is that it requires the framework edge version because several tweaks were brought to $f3->reroute recently. You can also wait for the next release (3.4.1) which should come soon.

planetahuevo commented 9 years ago

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!