How to get Stack Overflow SEO friendly URL structure in Nuxt js

0 votes
Stack Overflow has the following URL structure stackoverflow.com/questions/{question ID}/{question title}, and if you miss type the {question title} you will be permanently redirected 301 to the correct URL as long as you have the correct {question ID}.

Assuming that I have both the id and the slug, how can I make the same URL structure that Stack Overflow has in Nuxt.js with SSR?
Mar 10, 2022 in Digital Marketing by Kichu
• 19,040 points
1,807 views

1 answer to this question.

0 votes

The following worked for me :

To get the content from the database, I used async asyncData, and as you can access context, redirect, req, and res as parameters you can perform a 301 redirect.

In my folder, I used the unknown dynamic nested routes  like this: /pages/folder/_.vue. Which will catch all routes including domain.com/folder/{id} and domain.com/folder/{id}/{title}.

Split the pathMatch and get the first element, like this params.pathMatch.split('/')[0] to get the ID of in request URL. Then I use the id to get the content from the database, which is Strapi in my case. Like this await $strapi.findOne('contentType', id). 

Then like this : /folder/${data.id}/${data.slug} create the actual URL . Now you can match the requested URL with the actual URL. 

My entire code:

async asyncData({ redirect, req, route, app, $strapi, error, params }) {

    try {

        const recipeID = params.pathMatch.split('/')[0];

        const matchingRecipe = await $strapi.findOne('recipes', recipeID);

        const correctPath = `${app.localePath('recipes')}/${matchingRecipe.id}/${matchingRecipe.slug}`;

        if(route.fullPath !== correctPath) {

            console.log(`Redirect: ${route.fullPath} => ${correctPath}`);

            redirect(301, correctPath);

        }

        return {

            recipe: matchingRecipe

        }

    } catch(e) {

        console.log(e)

        error({ statusCode: e.statusCode, message: e.original });

    }

    

},
answered Mar 11, 2022 by narikkadan
• 63,600 points

Related Questions In Digital Marketing

0 votes
0 answers

Wordpress Plug-ins: How-to add custom SEO friendly URL Handles

I was wondering if its possible to ...READ MORE

Mar 5, 2022 in Digital Marketing by Kichu
• 19,040 points
651 views
0 votes
0 answers

How to over-ride meta data in Wordpress, using Squirrly SEO

I currently have a Wordpress website using ...READ MORE

Mar 4, 2022 in Digital Marketing by Kichu
• 19,040 points
587 views
0 votes
0 answers

.htaccess give 404 Not Found when url in SEO-friendly URL

Im using opencart fresh install and using ...READ MORE

Mar 4, 2022 in Digital Marketing by Kichu
• 19,040 points
1,156 views
0 votes
0 answers

How to make website more SEO friendly when the content is coming from the database? [closed]

How can i make my database content ...READ MORE

Mar 5, 2022 in Digital Marketing by Kichu
• 19,040 points
353 views
0 votes
1 answer

Issue with Open Graph tags and Nuxt.js (prod?)

I also faced the same issue. The ...READ MORE

answered Mar 11, 2022 in Digital Marketing by narikkadan
• 63,600 points
2,159 views
0 votes
1 answer
0 votes
1 answer

Googlebot is accessing .aspx pages, it should access SEO-friendly URLs only

google bot will find  it if you ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,600 points
484 views
0 votes
1 answer

Generate SEO friendly URLs (slugs)

function format_uri( $string, $separator = '-' ) {     $accents_regex ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,600 points
609 views
0 votes
1 answer

How to over-ride meta data in Wordpress, using Squirrly SEO

if (is_archive()){ add_filter('sq_title', 'filter_product_wpseo_metadesc'); function filter_product_wpseo_metadesc($wpseo_metadesc){ ...READ MORE

answered Mar 1, 2022 in Digital Marketing by narikkadan
• 63,600 points
868 views
0 votes
1 answer

How to remove home page title in Yoast SEO?

Here, you have two possibilities. You can ...READ MORE

answered Mar 4, 2022 in Digital Marketing by narikkadan
• 63,600 points
3,080 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP