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 in Digital Marketing by Kichu
• 19,040 points
626 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 by narikkadan
• 37,660 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 in Digital Marketing by Kichu
• 19,040 points
100 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 in Digital Marketing by Kichu
• 19,040 points
48 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 in Digital Marketing by Kichu
• 19,040 points
362 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 in Digital Marketing by Kichu
• 19,040 points
49 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 in Digital Marketing by narikkadan
• 37,660 points
875 views
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 in Others by narikkadan
• 37,660 points
124 views
0 votes
1 answer

Generate SEO friendly URLs (slugs)

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

answered Feb 14 in Others by narikkadan
• 37,660 points
131 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 in Digital Marketing by narikkadan
• 37,660 points
231 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 in Digital Marketing by narikkadan
• 37,660 points
622 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