Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

352
Views
Adding symbol (e.g. @) in front of slug in NextJS route

I am trying to add @ in front of a slug URL. For example: https://example.com/@username.

I tried @[username].js but that is not working.

Is this even possible?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try using rewrites and some regex. Next.js uses path-to-regexp under the hood.

  async rewrites() {
    return [
      {
        source: '/:userId(@[a-zA-Z0-9]+)/:id*',
        destination: "/user/:userId/:id*",
      }
    ]
  }

On client side use next/link:

<Link href={`@${userId}`}>
  <a>user</a>
</Link>
over 4 years ago · Santiago Trujillo Report

0

In your backend when you handle users, put the @ in the users' usernames. Then, simply treat the usernames as if they were plain text slugs. This works because @ is allowed in URLs, just like a, b or c.

Working example on StackBlitz.

pages/index.js:

import Link from 'next/link'

const username = '@foobar' // hardcoded example username, this will be recieved from your backend

export default function Index() {
    return (
        <Link href={username}>
            <a>{username}</a>
        </Link>
    )
}

pages/[username].js:

import { useRouter } from 'next/router'

export default function Username() {
    const router = useRouter()
    const { username } = router.query

    return <h1>User: {username}</h1>
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!