# Probleem met permalinks na migratie

**URL:** <https://meta.discourse.org/t/problem-with-permalinks-after-migration/176280>\
**Category:** Migration\
**Created:** [16 januari 2021 om 13:32 UTC](https://meta.discourse.org/t/problem-with-permalinks-after-migration/176280 "2021-01-16T13:32:23Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Göks](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/g%C3%B6ks/32/206285_2.png) [@Göks](https://meta.discourse.org/u/G%C3%B6ks)\
**Post date:** [17 januari 2021 om 11:59 UTC](https://meta.discourse.org/t/problem-with-permalinks-after-migration/176280/9 "2021-01-17T11:59:02Z")

</div>

Did not work as expected because the slug part of the URL was generated in the migration-script slighty different as in the old software.

So I “normalized” the URL by my own.

## Add/generate normalized URLs (Discourse Permalinks Table)

In the url-field get from

`forum/t140842-s1/8p-hilfe-bei-1-8-tfsi-guter-motor-oder-schlechter-motor.html`

or  
`forum/t140842-s2/8p-hilfe-bei-1-8-tfsi-guter-motor-oder-schlechter-motor.html`

or  
`forum/t140842/8p-hilfe-bei-1-8-tfsi-guter-motor-oder-schlechter-motor.html`

to a simple url with only the id of the old topic:  
`forum/t140842`

This is done by a SQL Command which rewrites the URL via the REGEXP\_REPLACE function:

`INSERT INTO permalinks (created_at, updated_at, topic_id, url) SELECT NOW(), NOW(), topic_id, REGEXP_REPLACE(url,'forum/t(\d*)(-?.*)/(.*)','forum/t\1','') url FROM permalinks WHERE topic_id > 0 ON CONFLICT DO NOTHING;`

## Rewrite old Requests via .htaccess on the old domain

`RewriteRule ^forum/t([0-9]*)(-?.*)/(.*)$ https://discourse-domain.com/forum/t$1 [R=301,L]`

## So what happens here?

Google has indexed and is linking to the URL `https://old-domain.com/forum/t140842/8p-hilfe-bei-1-8-tfsi-guter-motor-oder-schlechter-motor.html`. I was lucky that this request ended on an Apache server because of a different domain and i could use .htaccess to rewrite easily. So I am rewriting this request to `https://discourse-domain.com/forum/t140842`. In the permalinks table I added this `forum/t140842` record by using the already added permalink with the whole slugged url using a regex (see above).

Hope this helps somebody else as a starting point.

---

_[View the full topic](https://meta.discourse.org/t/problem-with-permalinks-after-migration/176280)._
