# Problem with permalinks, or regex?

**URL:** https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686
**Category:** Support
**Created:** [March 9, 2017, 12:34am UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686 "2017-03-09T00:34:59Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 9, 2017, 12:34am UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/1 "2017-03-09T00:34:59Z")

</div>

I’m trying to improve the permalink redirects on the vbulletin importer. Vbulletin can have any of several URL formats. My solution is to create generic permalinks which will then be handled by `permalink normalizations`. Like this:

Old URL: [http://somesite/forums/f38/slug-blah-blah-blue-11769.html](http://somesite/forums/f38/slug-blah-blah-blue-11769.html)  
Permalink: oldlinks/topics/thread-11769

Normalization: `/forums\/f[0-9]+\/.+-([0-9])+\.html.*/oldlinks\/topics\/thread-\1`

![](https://global.discourse-cdn.com/meta/original/3X/0/3/032496189b14a18a075e910e8bd3f5fd08a6639e.png)

It looks like it should work to me, but it’s not.

Have I missed a character somewhere?

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [March 9, 2017, 12:40am UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/2 "2017-03-09T00:40:46Z")

</div>

Trailing slash on the RE?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 9, 2017, 12:54am UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/3 "2017-03-09T00:54:51Z")

</div>

Thanks, @elijah. Are you saying I’m missing one, or that I’ve got an extra? In either case, it looks OK, right? I added an image of the normalizations since there’s no way to copy them.

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [March 9, 2017, 1:03am UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/4 "2017-03-09T01:03:25Z")

</div>

I’m good with REs, but that means I know there are a maze of similar looking implementations all with slightly different syntax. Your RE looks to be `/` delimited, but has no closing `/`:

`/forums\/f[0-9]+\/.+-([0-9])+\.html.*/oldlinks\/topics\/thread-\1/`

(And here I also wonder is it `\1` or `$1` for substitutions?)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 9, 2017, 4:12pm UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/5 "2017-03-09T16:12:02Z")

</div>

> [@elijah](#):
>
> Your RE looks to be / delimited, but has no closing /:

> [@elijah](#):
>
> (And here I also wonder is it \1 or $1 for substitutions?)

Here’s the most convenient answer to those questions:

![](https://global.discourse-cdn.com/meta/original/3X/c/d/cd4b7d6a7ba222ee0805904bddd6d09e508ec53c.png)

There’s no trailing `/` in their example (I think I included one in one of my earlier attempts), and yes, it’s `\1` to insert captures.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [March 9, 2017, 5:51pm UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/6 "2017-03-09T17:51:43Z")

</div>

> [@pfaffman](#):
>
> There’s no trailing / in their example

Isn’t there? I.e.  
`/(topic.*)\?.*/`

I can’t help but wonder, do you actually have URLs that have “.html” towards the end, and if you do, how often are they followed by zero or more nothing, anything, everything characters?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 9, 2017, 6:14pm UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/7 "2017-03-09T18:14:26Z")

</div>

I meant after the replacement string. I have a `/` at the end of my RE (right?), but not the end of the replacement.

> [@Mittineague](#):
>
> I can’t help but wonder, do you actually have URLs that have “.html” towards the end,

I don’t know. Perhaps not. In other systems I’ve seen various cruft after the URL (as the example replacement suggests). I’m probably safe removing that last `.*` to be sure.

####To recap:

Normalization: `/forums\/f[0-9]+\/.+-([0-9])+\.html/oldlinks\/topics\/thread-\1`

- works: [http://localhost:3000/oldlinks/topics/thread-11769](http://localhost:3000/oldlinks/topics/thread-11769)
- no joy: [http://localhost:3000/forums/f38/a-bunch-of-words-11769.html](http://localhost:3000/forums/f38/a-bunch-of-words-11769.html)

I’ve checked the regex at [http://regexr.com/](http://regexr.com/) & it seems to work there. I’ve done something similar in the past. I’m stumped.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 9, 2017, 8:11pm UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/8 "2017-03-09T20:11:08Z")

</div>

I thought that perhaps my problem was that I didn’t need to `\` escape  
the `/`s in my replacement string, but that doesn’t seem to have solved it either.

And I’ve confirmed that the regex works, it’s the replacement that’s broken. If I create a permalink badword-11769 and use this

```
/forums\/f[0-9]+\/.+-([0-9])+\.html/badword-\1

```

The redirect works fine.

Edit: No, it doesn’t.

Final edit:

```
WRONG: /forums\/f[0-9]+\/.+-([0-9])+\.html/oldlinks\/topics\/thread-\1
RIGHT: /forums\/f[0-9]+\/.+-([0-9]+)\.html/oldlinks\/topics\/thread-\1
                                  ^^

```

Sigh.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 8, 2024, 12:45pm UTC](https://meta.discourse.org/t/problem-with-permalinks-or-regex/58686/9 "2024-06-08T12:45:00Z")

</div>

This topic was automatically closed after 2648 days. New replies are no longer allowed.
