Advertisement:

Author Topic: Old links with html not working in nginx  (Read 920 times)

Offline spiros

  • Language Moderator
  • SMF Hero
  • *
  • Posts: 1,660
  • Gender: Male
  • A different point of view
    • spiros.doikas on Facebook
    • doikas on LinkedIn
    • @greektranslator on Twitter
    • Greek Translation
Old links with html not working in nginx
« on: May 29, 2018, 09:26:23 AM »
I have links in this format:

https://www.translatum.gr/forum/index.php/topic,2884.0.html

But when switching to nginx instead of apache they will lead to an error page outside the forum.

How can I either add a rule to accept those links in nginx or run a regex in the dB to change to standard format, I.e. from this format:

Code: [Select]
https://www.translatum.gr/forum/index.php/topic,2884.0.html
to this:

Code: [Select]
https://www.translatum.gr/forum/index.php?topic=2884.0

Offline GigaWatt

  • The Smiley Guy
  • Support Specialist
  • Sophist Member
  • *
  • Posts: 1,026
  • Gender: Male
    • Macedonian electronics forum
Re: Old links with html not working in nginx
« Reply #1 on: May 29, 2018, 10:12:24 AM »
You could probably do it with this script ;). Do a database backup first and leave $doit = 'No' (do a test run first ;)).

Note: It doesn't support wildcards (*).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Offline spiros

  • Language Moderator
  • SMF Hero
  • *
  • Posts: 1,660
  • Gender: Male
  • A different point of view
    • spiros.doikas on Facebook
    • doikas on LinkedIn
    • @greektranslator on Twitter
    • Greek Translation
Re: Old links with html not working in nginx
« Reply #2 on: May 29, 2018, 10:19:10 AM »
Quite complex script, not sure if it will fix the style of urls I mention.

I had something like this in mind:

Code: [Select]
UPDATE smf_messages SET body = REPLACE(body, 'oldURL', 'newURL') WHERE ID_BOARD = 1
but I am not sure of how to implement the REGEXP_REPLACE to match the URL style change (I am using MariaDB 10.2)

Offline GigaWatt

  • The Smiley Guy
  • Support Specialist
  • Sophist Member
  • *
  • Posts: 1,026
  • Gender: Male
    • Macedonian electronics forum
Re: Old links with html not working in nginx
« Reply #3 on: May 29, 2018, 10:44:28 AM »
Try the script I posted with these settings.

Code: [Select]
$oldURL = 'translatum.gr/forum/index.php/topic,';
$oldDir = '/12345';
$newURL = 'translatum.gr/forum/index.php?topic=';
$newDir = '/12345';
$doit = 'No';

Let it run, it'll probably change the URL you posted to something like this.

Code: [Select]
https://www.translatum.gr/forum/index.php?topic=2884.0.html
The problem is, how to remove the .html at the end now :S. Hmmm...

Maybe try another run of the script, but do it with these settings.

Code: [Select]
$oldURL = '.0.html';
$oldDir = '/12345';
$newURL = '.0';
$newDir = '/12345';
$doit = 'No';

Now this should fix the links to topics (ending with .0), but if there are some links that lead to a topic's page (.20, .30, .40), you'll probably have to fix them with more runs :S.
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Offline spiros

  • Language Moderator
  • SMF Hero
  • *
  • Posts: 1,660
  • Gender: Male
  • A different point of view
    • spiros.doikas on Facebook
    • doikas on LinkedIn
    • @greektranslator on Twitter
    • Greek Translation
Re: Old links with html not working in nginx
« Reply #4 on: May 29, 2018, 11:34:25 AM »
The problem is that there are also external URLs matching "0.html". These links will be broken.

Also, there are links to messages like these:

Code: [Select]
https://www.translatum.gr/forum/index.php/topic,696.msg2729.html#msg2729
For the time being, this is the best I could get at with 3 queries:

Code: [Select]
UPDATE smf_messages SET body = REPLACE(body, 'translatum.gr/forum/index.php/topic,', 'translatum.gr/forum/index.php?topic=') WHERE ID_BOARD = 27
UPDATE smf_messages SET body = REPLACE(body, '.0.html', '.0') WHERE ID_BOARD = 27
UPDATE smf_messages SET body = REPLACE(body, '.html#msg', '#msg') WHERE ID_BOARD = 27
« Last Edit: May 29, 2018, 12:28:57 PM by spiros »


Offline GigaWatt

  • The Smiley Guy
  • Support Specialist
  • Sophist Member
  • *
  • Posts: 1,026
  • Gender: Male
    • Macedonian electronics forum
Re: Old links with html not working in nginx
« Reply #6 on: May 29, 2018, 07:27:59 PM »
The problem is that there are also external URLs matching "0.html". These links will be broken.

Yeah, but how many of them end with a .0.html, not just 0.html. And in those rare cases, you could just copy the message IDs of those messages (the script reports the message IDs), load them one by one and correct them back to the previous value ;).

Also, there are links to messages like these:

Code: [Select]
https://www.translatum.gr/forum/index.php/topic,696.msg2729.html#msg2729
For the time being, this is the best I could get at with 3 queries:

Code: [Select]
UPDATE smf_messages SET body = REPLACE(body, 'translatum.gr/forum/index.php/topic,', 'translatum.gr/forum/index.php?topic=') WHERE ID_BOARD = 27
UPDATE smf_messages SET body = REPLACE(body, '.0.html', '.0') WHERE ID_BOARD = 27
UPDATE smf_messages SET body = REPLACE(body, '.html#msg', '#msg') WHERE ID_BOARD = 27

Also doable with the script I posted. The script will also do what your query does, except it will report every message that will be corrected by the script, which will give you a chance to see if there are any external links that will be corrected, but don't need to be corrected.

digger's solution is also good. It only works on a particular server setup, but hey, if you're not planning on changing hosts in the near future, it'll do ;).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Offline spiros

  • Language Moderator
  • SMF Hero
  • *
  • Posts: 1,660
  • Gender: Male
  • A different point of view
    • spiros.doikas on Facebook
    • doikas on LinkedIn
    • @greektranslator on Twitter
    • Greek Translation
Re: Old links with html not working in nginx
« Reply #7 on: May 30, 2018, 03:06:34 AM »
It was fixed with the queries I posted, you are right about the .0.html not many non-SMF urls with that. Many thanks for the help!

Just to make it clear (in case someone tries something similar in the future) the ones I posted were a test on a single board only; to apply throughout the forum just drop the

Code: [Select]
WHERE ID_BOARD = 27
bits.

Offline GigaWatt

  • The Smiley Guy
  • Support Specialist
  • Sophist Member
  • *
  • Posts: 1,026
  • Gender: Male
    • Macedonian electronics forum
Re: Old links with html not working in nginx
« Reply #8 on: May 30, 2018, 06:42:46 AM »
Glad you had it fixed ;).
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."