Tips for manipulating specific URL redirection to an alternative URL within a NuxtJs application

Take this scenario, where the inputted URL is:

http://localhost:3000/course-details

The desired outcome should be a redirection to http://localhost:3000/courses

I recall there being a method for achieving this, but it slips my mind at the moment.

Answer №1

To manage this situation efficiently, you can implement the necessary changes in the middleware of a vue-page-component.

Consider the following file: pages/course-details.vue

<script>
export default{
  middleware({ redirect }) {
    return redirect('/courses');
  }
}
</script>

In addition to using vue-page-component middleware, another option is to utilize nginx for handling such redirects.

location = /course-details {
  return 301 /courses;
}

Answer №2

@nuxtjs/router-extras provides a helpful library that enables more customizable options for routers, such as the ability to redirect to another URL.

/pages/course-details.vue

<router>
{
  redirect: '/courses',   // Another option: redirect: { name: 'courses' },
}
</router>

<template>
[...]

Alternatively, DengSihan's solution offers a solid vanilla approach.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: excessive recursion detected in <div ng-view="" class="ng-scope">

I've recently started learning angularJS and I've encountered an error that I need help with. Below is my index.html file: <body ng-app="myApp"> <div ng-view></div> <a href="table">click</a> <script ...

React.js, encountering unusual behavior while implementing a timer

I'm currently working on a React project to create a basic timer. The start button should initialize the timer, while the stop button should pause it. However, I've encountered some unexpected behavior. When I click on start for the first time, ...

What exactly does the term "library" refer to in the context of jQuery, a JavaScript

I'm confused about the concept of a library - when it comes to jQuery, can it be described as a large file containing multiple plugins that are pre-made and ready for use? ...

What is the best way to clear an array?

Yesterday I had a query regarding JSON Check out this link for details: How to return an array from jQuery ajax success function and use it in a loop? One of the suggested answers included this script: setInterval(updateTimestamps,30000); var ids = new ...

ways to display view without page refresh in mvc3

@using (Html.BeginForm("Index", "HRBankInfo", FormMethod.Get)) { <div align="center" class="display-label"> @ViewBag.message <br /><input type="submit" value="Ok" /> </div> } This particular partial view is display ...

What is the best way to create a JavaScript Up/Down Numeric input box using jQuery?

So, I have this Numeric input box with Up/Down buttons: HTML Markup: <div class="rotatortextbox"> <asp:TextBox ID="txtrunningtimeforfirstlot" ClientIDMode="Static" runat="server">0</asp:TextBox> (In mins) </div> <div cl ...

Unable to make changes to the text within the textarea field

Currently, I am in the process of creating a script to streamline the tedious task of providing teaching feedback. To scrape data such as student names and classes, I am utilizing selenium/python. While everything is running smoothly, I have encountered an ...

Error in npm installation: expected version "0.15.7" but received an empty string on the new Vite project

After setting up a new vite project, I encountered an error while trying to run npm install. Node.js version: 16.17.0 NPM version: 8.15.0 npm ERR! C:\xxx\GIT\NewTestProject\node_modules\esbuild\install.js:93 npm ERR! thr ...

Exploring the Depths of React Routing: The Power

I'm currently diving into React and trying to figure out how to create dynamic routes similar to partial pages in Angular. Here is my main App component: import React from 'react'; import Header from '../common/Header'; export d ...

Steps for resetting a div's display after adjusting the device size

I have a code that displays horizontally on desktop screens and vertically on phones. It includes an x button (closebtn) that is only displayed on phones to close the menu bar. How can I automatically display it again after resizing the page back to deskto ...

Angular2 Window Opener

Trying to establish communication between a child window and parent window in Angular 2, but I'm stuck on how to utilize window.opener for passing a parameter to Angular 2. In my previous experience with Angular 1.5, I referenced something similar he ...

Displayable components within choices in a dropdown menu

Is it possible to dynamically update select options based on the visibility of elements in the DOM that are controlled by v-if? The current code I have does not seem to be updating the options as elements are hidden. <select class="form-control-sm fl ...

Customizing object joining rules in JavaScript arrays

My array consists of different colored items with their respective types and amounts [ { color: 'blue', type: '+', amount: '1' }, { color: 'blue', type: '-', amount: '1' }, { color: 'blu ...

Undefined is returned after resolving - React - JavaScript API

Learning React and JavaScript is a bit challenging for me, especially when it comes to understanding how Promises and resolving work. I'm currently working on an API to log into an application with an internal SQL database. The queries are functionin ...

JavaScript function to provide a range of numbers based on a specified number and step value

I am looking for a solution to dynamically generate content based on the number of slides/steps using JavaScript. Any suggestions on how to best achieve this? Thanks in advance! switch(this.currentSlide) { case 1: return '1-12'; ...

Creating responsive tabs that transition into a drop-down menu for mobile devices is a useful feature for

I am looking to create a responsive tab design that drops down like the example shown below: Desktop/Large Screen View https://i.stack.imgur.com/hiCYz.png Mobile View https://i.stack.imgur.com/gRxLv.png I have written some code for this, but I am unsure ...

Sorting out conflicts during the compilation of a Vue project

I'm currently facing an issue with compiling my project. I have already attempted this solution, but after deploying my application, the toolbar and some other components lost their base style. Here is my current package.json. "dependencies&quo ...

What is the method for obtaining receipt numbers in sequential order, such as the format AB0810001?

Is AB the receipt code that should remain constant followed by the current date (08) and 10001 as the receipt number? ...

Creating individual product pages from an array of objects: A step-by-step guide

Is there a way in Next.js to create individual pages for each object in an array with unique URLs? Here is the input array: Input Array const products = [ { url: "item-1", id: 1, name: "Item 1", description: "lor ...

Tips for implementing Laravel authentication for an API while utilizing mongodb

For my latest project, I decided to develop a web app using Laravel, Vue.js, and MongoDB. When it came to creating the login functionality, I utilized the following code: <?php namespace App\Http\Controllers; use Illuminate\Http\R ...