开发者

Optional URL params that are automatically appended if not included

My site has articles and I would like the URL to be like so: example.com/(article id)/(article title)

There are two reason I would like the URLs like so, SEO, and readability.

This setup is close to how stackoverflow is set up. And just like on stackoverflow, I don't want the title to be required but if it's not including in the I want the back-end to append the title to the URL.

To understand what I mean, take the current page's URL, remove the title form it and go to that new url. If you notice when the page is refreshed the title was automatically appended to the URL.

When I think about how to achieve this, it requires two queries. The first q开发者_开发百科uery will get the title based on the id, then form the new url, and redirect to it. The second URL will query the database with the id again this time displaying the article.

Is this the only way? Can I append the extra param without actually redirecting?

Side note: I am using cakePHP


I think that yes, this is the only way.

Call #1 to example.com/(article id): the server would query the database for the id, see that the url isn't the canonical one[1] and do a redirect.

Call #2 to example.com/(article id)/(article title) would behave as normal, meaning some more queries to the database...

^ I suggest you store an article's generated url path so you could use it in other places too.
This way, if no title was provided, the path would just be "/(article id)" and you wouldn't need to do a redirect here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜