Hello Vercel Community,
I am currently trying to optimize my URL redirects to enhance SEO and user experience by minimizing redirect chains on my site hosted with Vercel and built with Next.js. I am experiencing some issues with how redirects are currently handled and need help configuring them correctly.
Current versus Expected behavior
Current Behaviour
Using the httpstatus.io tool, I’ve noticed that my domain’s HTTP requests undergo multiple redirects before reaching the final HTTPS non-www URL, which is not optimal. Here’s what happens when I test http://www.example.com
:
http://www.example.com
redirects tohttps://www.example.com
(Status 308)https://www.example.com
then redirects tohttps://example.com
(Status 308)
This behavior results in two redirect steps as illustrated in the screenshot below:
Here, by default, HTTP redirects directly to the HTTPS domain.
Expected Behaviour
I would prefer that any access to http://www.example.com
redirects directly to https://example.com
in a single step using a 301 status code for all redirects. This adjustment is crucial for maintaining link equity and SEO ranking. Here’s the behavior I aim for:
- Single redirect from any
www
version directly to the non-www HTTPS version with a 301 status code. The current setup uses a 308 status code
Challenges and Configuration Attempts
According to Vercel’s documentation, it is possible to define HTTP redirects within an application (screenshot attached). But have been unable to configure the desired behavior.
According to Vercel’s documentation, it should be possible to define such redirects. I attempted to use the following configuration in my vercel.json
:
However, this resulted in a ‘too many redirects’ error. Below is a snippet from the Vercel documentation I referenced: Configuring projects with vercel.json
{
"redirects": [
{
"source": "/(.*)",
"destination": "https://example.com",
"statusCode": 301
}
]
}
Documentation and Resources Consulted
Previous Community Post Found
I found a similar issue discussed here: Community Post, but it lacks a conclusive solution, which is why I am reaching out for additional insights.
Project information (URL, framework, environment, project settings)
- Nextjs v15.1.7
- WordPress API
- vercel (Hobby Version)
Thank you in advance for your help!