# When slug generation method is encoded, It redirected many times when we open new tab opens in topic

**URL:** <https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247>\
**Category:** Bug\
**Created:** [August 1, 2017, 1:51pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247 "2017-08-01T13:51:49Z")\
**Posts on this page:** 10\
**Page:** 1

## AI-generated summary

A discussion on Discourse's meta forum addresses a bug causing `ERR_TOO_MANY_REDIRECTS` when opening topics with non-English characters in their slugs. [Alavi1412](/t/-/67247/1) identifies that the issue arises from encoding mismatches between the slug stored in the database and the slug in the URL, particularly affecting Persian (RTL) titles. The problem stems from differences in encoding and potential whitespace in the slug, as shown by debugging outputs where `@topic_view.topic.slug` and `params[:slug]` differ in length and content. [fantasticfears](/t/-/67247/5) suggests using `.strip` to remove whitespace, but [Alavi1412](/t/-/67247/7) confirms it doesn't resolve the issue. [Pad_Pors](/t/-/67247/8) shares a temporary workaround involving changing the slug generation method and moving topics to reset their slugs. [mpalmer](/t/-/67247/6) notes the `params[:slug]` encoding is likely `ASCII-8BIT`, pointing to a broader encoding issue. [tgxworld](/t/-/67247/9) suggests the bug may be fixed in newer versions, specifically after upgrading to Rails 5.1.

<div class="post-metadata">

**Author:** ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)\
**Post date:** [August 1, 2017, 1:51pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/1 "2017-08-01T13:51:49Z")

</div>

Continuing the discussion from [ERR\_TOO\_MANY\_REDIRECTS After Rebuild](https://meta.discourse.org/t/err-too-many-redirects-after-rebuild/62111/6):

I found out that the error `ERR_TOO_MANY_REDIRECTS` after last update was because of non-english character in title. When I changed the `slug generation method` to none, and created a new topic(that doesn’t include non-english character in slug and url) it opens with no error but that previous topics has still the problem.

PAY ATTENTION to URL:

pages with error:

 ![with_error](https://global.discourse-cdn.com/meta/original/3X/0/2/02ed8b510c1f2147d6f30fc670ce93cb008c5bf1.png)

pages without error:

 ![no_error](https://global.discourse-cdn.com/meta/original/3X/4/7/47c34493dd6a097ab7064c0bbd022c5a7176dc1a.png)

So I think the problem is from the topic controller.

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [August 1, 2017, 1:54pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/2 "2017-08-01T13:54:24Z")

</div>

On your first screenshot the slug is after the topic\_id, is that normal?

---

<div class="post-metadata">

**Author:** ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)\
**Post date:** [August 1, 2017, 1:55pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/3 "2017-08-01T13:55:57Z")

</div>

Yes, This is because Persian is RTL language.

in fact the url is like this:

`http://localhost:3000/t/%D8%A2%DB%8C%D8%A7-%D8%B2%D8%A8%D8%A7%D9%86-%D9%81%D8%A7%D8%B1%D8%B3%DB%8C-%D9%86%D9%82%D8%B4%DB%8C-%D8%AF%D8%B1-%D8%B4%DA%A9%D9%84%E2%80%8C%DA%AF%DB%8C%D8%B1%DB%8C-%D8%AA%D9%81%DA%A9%D8%B1-%D8%A7%D8%AD%D8%B3%D8%A7%D8%B3%DB%8C-%D9%88-%DB%8C%D8%A7-%D8%B9%D9%82%D9%84%D8%A7%D9%86%DB%8C-%D9%85%D8%A7-%D8%AF%D8%A7%D8%B1%D8%AF%D8%9F/4790`

UPDATE:

I have done this to check the error in line 661 in definition of `slug_do_not_match` method:

first check:

```
puts params[:slug]
puts @topic_view.topic.slug
puts @topic_view.topic.slug != params[:slug]

```

for English slug the output was like this:

```
topic
topic
false

```

for Persian slug the output was like this:

```
مشکل-پیدا-کردن-ترجمه-ی-خوب
مشکل-پیدا-کردن-ترجمه-ی-خوب
true

```

and I have changed the checking code like this:

```
puts params[:slug].length
puts @topic_view.topic.slug.length
puts @topic_view.topic.slug != params[:slug]

```

and the out put was interesting:

English slug:

```
6
6
false

```

BUT Persian slug:

```
36
66
true

```

I found out that it may save some whitespace after the name of the Persian slug(because whitespaces aren’t visible in console)

---

<div class="post-metadata">

**Author:** ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)\
**Post date:** [August 1, 2017, 4:18pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/4 "2017-08-01T16:18:46Z")

</div>

seems related (same error in the admin panel):

> [@Tags does not work with Cyrillic](https://meta.discourse.org/t/tags-does-not-work-with-cyrillic/67217/2):
>
> Ooooooh, this is going to get messy. In theory, URLs can only contain 7-bit ASCII characters. In practice, everyone’s pretty much decided that percent-encoded UTF-8 is ok_hand, which is why, if you look in the “env” for the log message, you’ll see something like REQUEST\_URI /tags/%D0%B7%D0%B5%D0%BC%D0%BB%D1%8F/notifications (Which is the UTF-8 encoding for “земля”) So, the problem is occuring because the contents of params is being encoded as ASCII-8BIT, and while most everything manages …

---

<div class="post-metadata">

**Author:** ![fantasticfears](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fantasticfears/32/119608_2.png) [@fantasticfears](https://meta.discourse.org/u/fantasticfears)\
**Post date:** [August 1, 2017, 7:56pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/5 "2017-08-01T19:56:23Z")

</div>

Did you try `puts @topic_view.topic.slug != params[:slug].strip`

And what’s the `@topic_view.topic.slug.encoding` and `params[:slug].encoding`?

---

<div class="post-metadata">

**Author:** ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)\
**Post date:** [August 1, 2017, 10:57pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/6 "2017-08-01T22:57:41Z")

</div>

$20 says `params[:slug].encoding` is `ASCII-8BIT`. I think we need to fix this everywhere, as per [this topic](https://meta.discourse.org/t/automatic-encoding-of-parsed-url-params/67232).

---

<div class="post-metadata">

**Author:** ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)\
**Post date:** [August 3, 2017, 6:29am UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/7 "2017-08-03T06:29:41Z")

</div>

> [@fantasticfears](#):
>
> puts @topic\_view.topic.slug != params[:slug].strip

yes and the problem still exist. `.strip` method won’t solve it.

---

<div class="post-metadata">

**Author:** ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)\
**Post date:** [August 6, 2017, 2:49pm UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/8 "2017-08-06T14:49:49Z")

</div>

we had faced this problem in our site, and to solve it temporarily and for short time:

1- we changed the `slug generation method` in admin panel to none.  
2- then we created a temp category, and select all old topics with Persian slug, move them to that category, and bring them back!

this way the slug of all the topics became some simple topic-numbers without any Persian character. there’s a simpler way of using code lines in the link below, that I didn’t try:

> [@Automatic encoding of parsed URL params](https://meta.discourse.org/t/automatic-encoding-of-parsed-url-params/67232/13):
>
> Does editing the title on old topics fix the problem? If so, something like this might work on the rails console: Topic.all.each do |topic| topic.title = topic.title topic.save! end (Assigning topic.title re-generates the slug) Please please please take a backup before trying that, as I don’t know if it will have any unintended side effects. It might also take a long time to run depending how many topics you have…

---

<div class="post-metadata">

**Author:** ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)\
**Post date:** [September 29, 2017, 4:26am UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/9 "2017-09-29T04:26:21Z")

</div>

@Alavi1412 Can you upgrade to latest? This is fixed after we upgraded to Rails 5.1

---

<div class="post-metadata">

**Author:** ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)\
**Post date:** [November 16, 2017, 9:04am UTC](https://meta.discourse.org/t/when-slug-generation-method-is-encoded-it-redirected-many-times-when-we-open-new-tab-opens-in-topic/67247/10 "2017-11-16T09:04:33Z")

</div>

Closed on November 16, 2017, 9:04am UTC


