OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

window.location.href doesn't adding referer header when executed inside html wrapper but running that in console is adding referer header. Why?

  • Thread starter Thread starter Yash Nigam
  • Start date Start date
Y

Yash Nigam

Guest
I have two subdomains, let's call them x.example.com and y.example.com. When a user requests x.example.com, the server redirects them to y.example.com. On y.example.com, there's a feature that displays specific content if the referer header matches a certain value. However, when the request comes to y.example.com after the redirection, it doesn't have any referer header attached to it. I'm using Django on the backend to handle the redirection with HttpResponseRedirect("y.example.com").

I attempted to include the referer header in the server's response using Django, but it didn't seem to have any effect.

Code:
response  = HttpResponseRedirect("y.example.com")
response['referer'] = 'https://x.example.com'

Additionally, I tried implementing a redirection in JavaScript by returning HTML with embedded JavaScript code, but that also didn't result in the referer header being passed along. Interestingly, directly executing the JavaScript redirection in the browser console did include the referer header. I'm puzzled as to why it's not working as expected. Can you suggest an alternative approach to achieve the desired behavior?

Code:
html_content = """
            <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redirecting...</title>
</head>
<body>
    <h1>Redirecting...</h1>
    <script>
        // JavaScript code for redirection
        window.location.href = 'https://y.example.com';
        });
    </script>
</body>
</html>
            """
# Return the HTML response with embedded JavaScript
return HttpResponse(html_content)

<p>I have two subdomains, let's call them x.example.com and y.example.com. When a user requests x.example.com, the server redirects them to y.example.com.
On y.example.com, there's a feature that displays specific content if the referer header matches a certain value. However, when the request comes to y.example.com after the redirection, it doesn't have any referer header attached to it.
I'm using Django on the backend to handle the redirection with HttpResponseRedirect("y.example.com").</p>
<p>I attempted to include the referer header in the server's response using Django, but it didn't seem to have any effect.</p>
<pre><code>response = HttpResponseRedirect("y.example.com")
response['referer'] = 'https://x.example.com'
</code></pre>
<p>Additionally, I tried implementing a redirection in JavaScript by returning HTML with embedded JavaScript code, but that also didn't result in the referer header being passed along. Interestingly, directly executing the JavaScript redirection in the browser console did include the referer header. I'm puzzled as to why it's not working as expected. Can you suggest an alternative approach to achieve the desired behavior?</p>
<pre><code>html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Redirecting...</title>
</head>
<body>
<h1>Redirecting...</h1>
<script>
// JavaScript code for redirection
window.location.href = 'https://y.example.com';
});
</script>
</body>
</html>
"""
# Return the HTML response with embedded JavaScript
return HttpResponse(html_content)
</code></pre>
 

Latest posts

Top