Invalid character in header content [“Location”]
Chances are that if you are working with urls that contains character other then english you might have encountered this error.
Recently I encountered this error while redirecting the user inside the getServerSideProps
.
return {
redirect: {
destination: `/user/${getPath()}`,
permanent: true,
},
};
The above snippet works fine with for the user if getPath returns a url path with english characters, but in my case it had some arabic characters because of it, I I
was getting this error. When I manually typed the arabic url in the browser it was working fine but within the getServerSideProps
it was throwing this error.
The solution to the problem is simple: wrap the return value from getPath
inside the encodeURIComponent
return {
redirect: {
destination: `/user/${encodeURIComponent(getPath())}`,
permanent: true,
},
};