Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias support #5

Closed
daebak74 opened this issue Aug 24, 2019 · 10 comments
Closed

Alias support #5

daebak74 opened this issue Aug 24, 2019 · 10 comments
Labels

Comments

@daebak74
Copy link

Hi,
there is any methosor way for manage routes with alias ?

for example convert

www.domain.com/post/:id/reader

to

www.domain.com/post/:alias

Thanks.

@ocram
Copy link
Contributor

ocram commented Aug 26, 2019

Currently, only the following two approaches are available:

  • Assign the same callable parameter to both routes. If you reference static methods or instance methods with a string or array, this is easy and does not duplicate any code.
  • If the request method for the route is GET (Router#get), you can cause a redirect.

Does that help?

Do you feel we need something else? Perhaps a separate Router#getAlias, Router#postAlias, etc. for all request methods that takes both the old and the new route and nothing else?

@daebak74
Copy link
Author

I would to understand what is the best approach to developing this case.
Make simple and for example simple posts archive each single post has unique id and alias for produce a seo friendly url.

id: 9, alias: my-first-post

The normal get request should be www.domain.com/post/:id/reader what should be the result www.domain.com/post/my-first-post and generate a sitemap.xml file. I should directly to create a route using the alias instead of id www.domain.com/post/:alias ?

It's correct the approach or there is a better way ?

Thanks.

@ocram
Copy link
Contributor

ocram commented Aug 27, 2019

It’s common practice to include both the ID and the alias:

www.example.com/posts/:id/:alias

// or

www.example.com/posts/:alias/:id

Of course, it’s up to you whether you want to check the correctness of the alias. Usually, wrong aliases are accepted but redirected to the correct one.

What do you need the routing component to do?

@daebak74
Copy link
Author

Yes id and aliar is the correct way.
Anyway what I looking for is

www.example.com/posts/:id/:alias -> www.example.com/posts/my-first-post

where my-first-post is the alias.

In sitemap I have to generate link with alias so I need that if in the browser is type

www.example.com/posts/my-first-post

show me the correct one.

Thanks.

@ocram
Copy link
Contributor

ocram commented Aug 29, 2019

So you also want to identify posts only based on their alias, without any ID at all? Do you even need the ID then?

And what do you want to do? Redirect from one to the other? Which direction? Or do you want to support both routes and handle them in the same way?

@daebak74
Copy link
Author

This is my first test to build a frontend project with your framework so I know what should be the result but I don't know the way for this I am asking. Most of the CMS system instead to show sitemap with details/id they show the alias for be SEO friendly. I suppose that is an url rewrite or... I don't know what is the best way for don't compromise the indexing.

In the other from the management point of view it is certainly better to use the ids instead of the aliases that even with all the controls there could be some inconsistencies.

So for this my question... what is the good practise for manage the route for has url seo friendly ?
If is out of the topic please close the post.

Thanks.

@ocram
Copy link
Contributor

ocram commented Aug 29, 2019

Well, let’s discuss the most common practice using a popular example:

https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git

As you can see, the form is:

https://stackoverflow.com/questions/:id/:alias

This approach is easy to manage and also good from a SEO perspective, so it’s a best practice to do it like this. You don’t need multiple versions or endpoints for this. Just always include both the ID and the display name.

As for generating the display names, there are libraries for this:

@daebak74
Copy link
Author

Thanks Great!
for slug I wrote this function anyway I'll give a look to your link.

function create_slug($string){
   $slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}

for sitemap I'll generate an XML with a SELECT and I'll add it in cron job or I'll think some other solution.

Thank you very much you leave me a doubt if was penalized that solution or not. :)

@ocram
Copy link
Contributor

ocram commented Aug 29, 2019

You will quickly find that there are tons of “edge cases”, or really any not-just-ASCII words, where the libraries will provide better results than your simple solution, which is fine otherwise.

As for the sitemap, you can either generate it on the fly just when it’s requested, or pre-generate it and store the result somewhere.

@ocram ocram closed this as completed Aug 29, 2019
@daebak74
Copy link
Author

Yes I just considered normal case but checking your link I saw that is better and complete. (
inexperience is the basis of knowledge) :)

I don't know if is your preference or a case that you put this library at first position, tomorrow I'll check better but I like the approach of the first one. (https://github.com/ausi/slug-generator)

For sitemap I was thinking to generate a file I don't know if update by cron job or just when change the page structure. Already I added extra field for include and exclude page or allowed or not bot.

Thanks again for your time and rapid support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants