# Redirect center with Django

**URL:** <https://forum.djangoproject.com/t/redirect-center-with-django/25474>\
**Category:** Getting Started\
**Created:** [November 20, 2023, 1:29pm UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474 "2023-11-20T13:29:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpvro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dpvro/32/17215_2.png) [@dpvro](https://forum.djangoproject.com/u/dpvro)\
**Post date:** [November 20, 2023, 1:29pm UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474/1 "2023-11-20T13:29:19Z")

</div>

I am interested to create a website using the latest Django version (\> 5.0 when is released) to obtain the following:

- I will create QR codes that will point to: mydomain dot com/c1p1i1 or mydomain dot com/c1p2i1.
- Some of them should be redirected to external addresses (extern to [domain.com](http://domain.com)) and some others should be redirected to my homepage (mydomain dot com).
- For all the requests (either redirected forward or to my homepage) I want to create a statistic to know how many requests I have received or to integrate google analytics and see statistics.

I have an idea how is more efficient to do it but I am interested if somebody has done something similar and could give me some hits to make these redirects really fast and also catch all the metadata for statistics.

Thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 20, 2023, 1:39pm UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474/2 "2023-11-20T13:39:55Z")

</div>

> [@dpvro](#):
>
> Some of them should be redirected to external addresses (extern to `domain.com`)

How I would do this would depend a lot upon the specific details. How many different QR codes (urls) are involved? How many external sites?

For the ones referencing internal resources (inside `mydomain.com`), how may URLs are being referenced? Is it important that they _redirect_ to your home page, or can it just resolve to your homepage?

If the number of URLs (QR codes) is relatively reasonable and bounded, I’d do this within nginx - Django wouldn’t even be involved here. You’ll have access to the individual references in your nginx logs.

---

<div class="post-metadata">

**Author:** ![dpvro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dpvro/32/17215_2.png) [@dpvro](https://forum.djangoproject.com/u/dpvro)\
**Post date:** [November 20, 2023, 1:58pm UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474/3 "2023-11-20T13:58:51Z")

</div>

Thank you for reply.

- I would say most probably between 200 and 300 QR codes with different parameters
- Around 100 external addresses (outside of [mydomain.com](http://mydomain.com))
- For the internal resources, I want to have only one page, my homepage. Is like a public personal profile which should be seen when someone accesses mydomain or scans a QR code that is intended to go internal.
- Redirect or Resolve, no preference, depends which option is faster and safer.
- Nginx would also be a possibility but needs more work to handle the statistics and by using Django and Django admin might sound easier to implement this (not sure, just a hunch)

I might also decide in the future to offer more functionality for my website (besides the homepage with the personal profile) and this is why I tend to use Django not nginx as a solution to this particular problem.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 20, 2023, 3:08pm UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474/4 "2023-11-20T15:08:01Z")

</div>

> [@dpvro](#):
>
> Around 100 external addresses (outside of `mydomain.com`)

That alone makes the nginx-based solution impractical. (It could be done, but I wouldn’t want to have to manage it.)

> [@dpvro](#):
>
> Nginx would also be a possibility but needs more work to handle the statistics and by using Django and Django admin might sound easier to implement this (not sure, just a hunch)

Notwithstanding the previous comment, not really. There are a _lot_ of resources already existing to provide analysis of web server logs. This is a well-covered field among those who manage web servers.

If I had to handle something like this, I’d first do something like define the urls such that nginx could _easily_ determine whether it’s an internal or external reference. (It doesn’t mean I necessarily am going to take advantage of that information yet, but I’d be inclined to believe it may have value.)

I’d then create middleware that would examine the incoming URL to short-circuit the normal process for external links. I’d send the request to a celery task and return the redirect. (I don’t want the redirect to have to wait for the request to be saved to the database.) Actually, I would do that logging in celery for the internal links coming in from QR code URLs as well.

---

<div class="post-metadata">

**Author:** ![dpvro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dpvro/32/17215_2.png) [@dpvro](https://forum.djangoproject.com/u/dpvro)\
**Post date:** [November 20, 2023, 3:59pm UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474/5 "2023-11-20T15:59:58Z")

</div>

Good idea with the middleware and celery. I might even search for something simpler than celery.

What could be the advantage to setup nginx to understand if it is an internal or external reference?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 20, 2023, 4:07pm UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474/6 "2023-11-20T16:07:51Z")

</div>

Depending upon a number of different factors, you _might_ want to consider making the external URL handler a separate app within your project. Have one app that only handles external URLs and a separate app that handles the internal URLs.

It’s an option for somewhat simplifying your application. Instead of having the middleware making that determination, it still allows nginx to do what _it_ does best, reducing the latency and load on your application. (Adding the middleware is going to have _some_ effect on your entire project - even for pages not coming from QR-code-URLs. Again, this _may_ not be an issue at all for you.)

> [@dpvro](#):
>
> I might even search for something simpler than celery.

There’s not a whole lot that would be simpler than celery using redis as the message broker - at least not an option that is reliable and performant.

---

<div class="post-metadata">

**Author:** ![dpvro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dpvro/32/17215_2.png) [@dpvro](https://forum.djangoproject.com/u/dpvro)\
**Post date:** [November 21, 2023, 8:14am UTC](https://forum.djangoproject.com/t/redirect-center-with-django/25474/7 "2023-11-21T08:14:13Z")

</div>

Thank you for the suggestions. They have truly helped me in shaping the structure of my project.
