Rewriting Next.js with custom headers

My web app allows users to create their own profiles with custom usernames, which can be accessed via the following URLs.

ourplatform.com/john
ourplatform.com/john/about
ourplatform.com/john/contact

ourplatform.com/jane
ourplatform.com/jane/about
ourplatform.com/jane/contact

Users also have the option to link their domain names to their profiles, resulting in URLs structured like this.

john.com
john.com/about
john.com/contact

jane.com
jane.com/about
jane.com/contact

Here is an excerpt from my next.config.js file.

module.exports = {
  async rewrites () {
    return [
      {
        source: '/:path*',
        has: [
          {
            type: 'header',
            key: 'x-username',
            value: '(?<username>.*)'
          }
        ],
        destination: '/:username/:path*'
      }
    ]
  }
}

In addition, here is the relevant Nginx configuration.

server {
  listen 80;
  server_name john.com;

  add_header x-username "john";
  proxy_set_header x-username "john";

  location /_next {
    proxy_pass http://127.0.0.1:1323;
  }

  location = / {
    proxy_pass http://127.0.0.1:1323/john;
  }

  location / {
    proxy_pass http://127.0.0.1:1323/john$request_uri;
  }
}

Despite these configurations, I am encountering a 404 error on the linked domains. Can anyone pinpoint what might be causing this issue?

Answer №1

Consider using host matching as an alternate approach which is explained in detail here

module.exports = {
  async rewrites() {
    return [
      {
        source: '/:path*',
        has: [{ type: 'host', value: 'john.com' }],
        destination: '/john/:path*',
      },
      {
        source: '/:path*',
        has: [{ type: 'host', value: 'jane.com' }],
        destination: '/jane/:path*',
      },
    ]
  },
}

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

drag items on your smartphone with ease

Hello everyone, I am looking to make items draggable on a smartphone as well. Here is my HTML code: <input class="inputText mb-2 border border-primary rounded" v-model="newTodo" @keypress.13='addTodo' placeholder="W ...

Datatables stands out by emphasizing rows across all paginated pages

Encountering an issue with the Datatables plugin when attempting to highlight rows on paginated pages beyond the first one. In the JavaScript code below, you can see where I have commented out adding the class info to all rows. When this is done and you n ...

Can Jquery be utilized to signal a language change?

I'm working on my portfolio website that will be available in two languages, Thai and English. I have successfully implemented language buttons for changing between the two languages. However, I am facing an issue with the language selection when nav ...

Exploring LZMA compression in conjunction with next.js

Is anyone familiar with using the lzma algorithm to compress video, photo, text, etc. in the Next.js framework? I have searched everywhere but couldn't find a solution. I haven't found a proper demonstration or answer anywhere. I would greatly a ...

Understanding @@iterator in JavaScript: An in-depth look

Can someone shed some light on the mysterious @@iterator? It keeps popping up in tutorials but no one seems to provide a clear explanation of what it actually is. Is it a symbol literal or something else entirely? ...

Tips for circumventing CORS in an ajax request using various browser extensions or add-ons

I am encountering a cross-origin problem with my WCF service. Despite adding an extension in chrome to bypass CORS, the issue persists. Is there any other extension/add-on/plugin available to test the service locally and bypass the CORS problem? Edit Aft ...

The values are not being displayed in the local storage checkbox

I attempted to transfer checkbox values to another HTML page using local storage, however they were not appearing on the other page Here is my page1 HTML code snippet: <input class="checkbox" type="checkbox" id="option1" name="car1" value="BMW"> ...

Requirements detailed in package.json

Suppose we have a client-side application (such as an Ember app). We define the package.json file for our application with its dependencies listed. { name: "my-app", dependencies: { "dep1" : "1.0.0" }, devDependencies: { ...

What is the designated location for custom components in the file structure of Next.js 13?

Is it necessary to create a Components/ directory within the app folder with this version? Or can new components be added anywhere in the file structure? Currently, I have a components/ folder nested inside the app/ and it seems to be functioning correctl ...

"Although the ajax request was successful, the post data did not transfer to the other

i am working with a simple piece of code: var addUser = "simply"; $.ajax({ type: 'POST', url: 'userControl.php', data: {addUser: addUser}, success: function(response){ alert("success"); } }); on the page use ...

Using node.js for synchronous callbacks in node.js programming

I am new to node.js, and from what I've gathered, each callback creates a new event that can be executed in parallel. For instance, consider the following code with a callback: function testFunction(var1){ s3.head(var1, function(data, err){ ...

When the add button is clicked, I would like to implement a feature where a checkbox is added

When the user clicks on the link "출력하기", I want the web page to add a checkbox to all images. I wrote this code, but it's not working. Can anyone help me? This is my JS: $(document).ready(function(){ $("#print").on('click', fu ...

Unable to transfer AJAX data to PHP script

Trying to figure out how to send AJAX data to PHP. While I am comfortable with PHP, JavaScript is a bit new to me. Incorporating HTML / JavaScript <input type="text" id="commodity_code"><button id="button"> = </button> <script id="s ...

Unable to transform Symbol data into a string - Error when making a React AJAX delete call

I'm encountering an issue where I am getting a Cannot convert a Symbol value to a string error in the console. My tech stack includes React v15 and jQuery v3. https://i.stack.imgur.com/qMOQ8.png This is my React code snippet: var CommentList = Reac ...

The Next.js application is functioning perfectly in the development environment. However, an error is encountered when running `npm run build

Encountering an error during the build process of my Next.js app. Generating static pages (0/20) [= ]TypeError: u is not a function at m (C:\Users\coool\Documents\AnalyticsBrewery\analytics-brewery-a1\.next\server&bso ...

All elements in the array are being simultaneously updated with the same value in React

I am encountering an issue with my code. Whenever I draw rectangles by clicking and dragging, the new rectangle added to the array overwrites all previously stored rectangles. For example, if my array (named data) initially contains Rectangles as - [Rect ...

Is there a way to adjust the label elevation for a Material UI TextField component?

I am trying to enhance the appearance of a textfield label, but I am facing some challenges with my code. textStyle: { backgroundColor: '#1c1a1a', border: 0, borderRadius: 0, width: 400, height: 66, display: 'flex&a ...

Interactive JQuery plugin for scrolling through thumbnails with customizable buttons

I am attempting to create a jQuery thumbnail scroller with buttons that respond to mousedown and mouseup events. I have successfully implemented the scrolling functionality, but I am facing issues when trying to refactor it into a reusable function. Below ...

Utilizing JQuery for a smooth animation effect with the slide down feature

I have a question about my top navigation bar animation. While scrolling down, it seems to be working fine but the animation comes with a fade effect. I would like to achieve a slide-down effect for the background instead. Since scrolling doesn't trig ...

Production environments may encounter difficulties displaying NextJS images stored on AWS S3

While developing a web app, I encountered an issue with loading images from an S3 bucket. In the development environment, everything works fine and the images load without any problems from their external URLs. However, once deployed, all the images defaul ...