Skip to content

Commit

Permalink
Merge pull request #2730 from owid/redirects-trailing-slash
Browse files Browse the repository at this point in the history
fix(redirects): always remove trailing slash
  • Loading branch information
marcelgerber authored Oct 11, 2023
2 parents f2622c9 + af89ea4 commit f17a8ae
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions baker/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const getRedirects = async () => {
const staticRedirects = [
// RSS feed
"/feed /atom.xml 302",
"/feed/ /atom.xml 302",

// Entries and blog (we should keep these for a while)
"/entries / 302",
Expand All @@ -22,6 +21,9 @@ export const getRedirects = async () => {

// Dynamic redirects are all redirects that contain an asterisk
const dynamicRedirects = [
// Always remove trailing slash
"/*/ /:splat 301",

// TODO: this should only get triggered by external hits (indexed .pdf files for instance)
// and should be removed when no evidence of these inbound links can be found.
"/wp-content/uploads/* /uploads/:splat 301",
Expand Down Expand Up @@ -55,16 +57,23 @@ export const getRedirects = async () => {
"/grapher/exports/* https://assets.ourworldindata.org/grapher/exports/:splat 301",
]

const formatWpUrl = (url: string) => {
if (url === "/") return url

return url
.replace(/__/g, "/") // replace __: abc__xyz -> abc/xyz
.replace(/\/$/, "") // remove trailing slash: /abc/ -> /abc
}

// Redirects from Wordpress admin UI
const wpRedirectRows = await wpdb.singleton.query(
`SELECT url, action_data, action_code FROM wp_redirection_items WHERE status = 'enabled'`
)
const wpRedirects = wpRedirectRows.map(
(row) =>
`${row.url.replace(/__/g, "/")} ${row.action_data.replace(
/__/g,
"/"
)} ${row.action_code}`
`${formatWpUrl(row.url)} ${formatWpUrl(row.action_data)} ${
row.action_code
}`
)

// Redirect old slugs to new slugs
Expand Down

0 comments on commit f17a8ae

Please sign in to comment.