ChatGPT en .htaccess: migreren zonder hoofdpijn
Gepubliceerd op 30 januari 2024 • 8 min leestijd • 1.599 woorden
ChatGPT in de Praktijk: Website Migratie en Redirects via .htaccess
ChatGPT en generatieve AI in het algemeen komt steeds meer in het nieuws. Je leest steeds vaker geluiden dat het slecht zou zijn voor de werkgelegenheid. Twee regels later lees je dat AI de uitkomst is voor de krapte op de arbeidsmarkt. Nog eens twee regels later lees je dat AI (of de mensen erachter?) het niet zo nauw neemt met rechtmatigheid van bronnen waarop het getraind is en met hetzelfde gemak een ‘origineel’ werk kan genereren, inclusief de handtekening van de schilder of auteur. Al die dingen wil ik het nou net niet over gaan hebben.
Waar ik het wel over wil hebben, is hoe je AI kunt inzetten voor alledaagse problemen. In dit geval hoe ik ChatGPT heb gebruikt om de migratie van Wordpress naar Hugo te ondersteunen.
Mijn probleem
In Google Search Console kun je zien welke links er van je website bekend zijn en door Google regelmatig worden bezocht. Als Google deze pagina’s bezoekt, wil dat ook zeggen dat ze, als ze goed bevonden worden, vindbaar zijn op Google.
Aangezien een aantal url’s niet meer aanwezig waren op de site, moesten deze omgeleid worden. Ik wilde geen enkele traffic missen die doorgestuurd werd door de grote zoekmachines, want ik had wel eens een artikeltje geschreven..
Oorspronkelijke link die ik door wilde sturen.
https://ajelsinga.nl/wp-login.php?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/Je kunt op Apache servers relatief eenvoudig url’s omleiden door gebruik te maken van een RewriteRule. Je geeft aan op welk deel van de url de RewriteRule actief moet zijn en waar deze dan naar doorgestuurd moet worden. Appeltje-eitje zou je zeggen.
Nou, in dit geval was dat wel een dingetje. Mijn kennis van RegEx is nou niet heel erg diepgaand. Na uren zoeken, prutsen, documentatie lezen en opnieuw prutsen was ik nog niet veel opgeschoten. Ook allerlei regex generators boden weinig soelaas. Het lukte wel om een deel van de url af te snoepen, zodat mijn url wat korter was, ```
https://ajelsinga.nl/?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution```, maar het stukje achter redirect_to kreeg ik maar niet voor elkaar.
Toch nog wat ‘slimme’ regex dingen geprobeerd, maar het laatste stukje lukte maar niet. Wel werd ik getrakteerd op allerlei HTTP500 foutmeldingen, aangezien de RewriteRule vaak niet geldig was.
ChatGPT to the rescue
In een vlaag van verstandsverbijstering dan wel wanhoop ChatGPT maar gevraagd:
generate the .htaccess rewrite rules to 302-redirect https://ajelsinga.nl/?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/ to /video-friday-jung-smart-house-solution/En tot mijn verbazing, en blijdschap, kreeg ik een antwoord waar ik wel iets mee kon. Mooier nog, ik kreeg zelfs voorgeschoteld wat iedere regel exact betekende.
To create a 302 redirect for the specific query string
?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/to/video-friday-jung-smart-house-solution/using an .htaccess file, you can use theRewriteEngineandRewriteConddirectives. Here’s how you would write the rule in your.htaccessfile:
apache
RewriteEngine On
RewriteCond %{QUERY_STRING} ^redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/$
RewriteRule ^/?$ /video-friday-jung-smart-house-solution/ [R=302,L]Here’s a breakdown of what each line and directive does:
RewriteEngine On: This line enables the rewrite module within the .htaccess file.
RewriteCond %{QUERY_STRING} ^redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/$: This line checks if the query string matches exactlyredirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/. The^and$are regular expression characters that match the beginning and end of the string, respectively, to ensure the entire query string matches.
RewriteRule ^/?$ /video-friday-jung-smart-house-solution/ [R=302,L]: This line redirects the request to/video-friday-jung-smart-house-solution/with a 302 HTTP status code if the condition set byRewriteCondis true. The^/?$part of theRewriteRuleensures that the rule only applies to the root of the domain (https://ajelsinga.nl/) with an optional trailing slash. The[R=302,L]flags indicate that the redirect is temporary (302) and that this should be the last rule (L) applied if the condition matches.Make sure to test this code after adding it to your .htaccess file to make sure that the redirect works as expected. Also, remember to clear the cache of your browser or test using incognito mode to see the changes immediately, as browsers tend to cache .htaccess redirects.
Maar goed, dit was natuurlijk nog maar het halve antwoord. Één url kon nu worden doorverwezen. En al die anderen dan? Geen nood:
create a 302 redirect from the above where /video-friday-jung-smart-house-solution/ can be any stringTo create a .htaccess 302 redirect rule where
/video-friday-jung-smart-house-solution/can be any string after?redirect_to=https://ajelsinga.nl/, you can use a more general regular expression. Here’s how you would adjust the .htaccess rule:
apache
RewriteEngine On
RewriteCond %{QUERY_STRING} ^redirect_to=https://ajelsinga.nl/(.*)$
RewriteRule ^/?$ /%1? [R=302,L]
>```
>
Here's an explanation of the changes:
>
> - `RewriteCond %{QUERY_STRING} ^redirect_to=https://ajelsinga.nl/(.*)$`: This line is checking if the query string starts with `redirect_to=https://ajelsinga.nl/` followed by any string. The `(.*)` is a regular expression that matches any character sequence (except newlines) and captures it for later use.
>
> - `RewriteRule ^/?$ /%1? [R=302,L]`: This line redirects the request to the captured string from the query string. `%1` refers to the first capture group in the `RewriteCond` above, which in this case is the string after `redirect_to=https://ajelsinga.nl/`. The `?` at the end of the rewrite rule is used to discard the original query string in the redirected URL.
>
With this rule, any URL in the form of `https://ajelsinga.nl/?redirect_to=https://ajelsinga.nl/any-string-here` will be redirected to `https://ajelsinga.nl/any-string-here` with a 302 HTTP status code. Remember to replace "any-string-here" with the actual path you want to redirect to.
>
As always, after implementing this change to the .htaccess file, test it thoroughly to ensure it's behaving as intended. If you have existing rewrite rules, the order of rules can affect the outcome, so be cautious with the placement of new rules.
{.quote}
Ik dit snel in de .htaccess van de site gezet. Joepie, het werkt!
Ineens bedacht ik me dat de oorspronkelijke regel uit Google Search Console nog een een stukje bevatte. Dus mijn buddy weer gevraagd:will https://ajelsinga.nl/wp-login.php?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/ also match the rule?
Het antwoord wat ik kreeg was wederom super. Niet alleen kreeg ik antwoord op mijn vraag, maar ik kreeg zelfs een verbeterde set regels die ik zo kon knippen plakken, uiteraard nadat ze getest waren.
>No, the rule provided will not match the URL ```
https://ajelsinga.nl/wp-login.php?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/``` because the `RewriteRule` pattern `^/?$` specifically targets the root of the domain with no path (i.e., 'https://ajelsinga.nl/'). The `wp-login.php` path would not match this pattern.
>
If you also want to include cases where other paths like `wp-login.php` can have a query string with `redirect_to=`, you need to modify the `RewriteRule` to match any path. Here's how you can adjust the rule:
>
>apache RewriteEngine On RewriteCond %{QUERY_STRING} (^|&)redirect_to=https://ajelsinga.nl/(.*) /%2? [R=302,L]
This adjusted configuration includes:
RewriteCond %{QUERY_STRING} (^|&)redirect_to=https://ajelsinga.nl/(.*)$: This line now allows for theredirect_toparameter to be anywhere in the query string, by allowing for either the start of the query string^or an ampersand&beforeredirect_to.
RewriteRule ^(.*)$ /%2? [R=302,L]: The rule is now modified to match any path given by the(.*)pattern and redirect it using%2, which corresponds to the second capture group of the condition. The query string is discarded as before.With the adjusted rule, both the root path (e.g.,
https://ajelsinga.nl/?redirect_to=...) and other file paths (e.g., ```
https://ajelsinga.nl/wp-login.php?redirect_to=...```) with the redirect_to parameter will be redirected to the value specified by the redirect_to parameter, and the redirection will be a 302 Temporary Redirect.
Proceed with caution when using .htaccess rules and employ thorough testing to make sure the rules don’t have unintended consequences.
Yes! Dit was de oplossing. Alles werd netjes geredirect. Het laatste wat me te doen stond, was het aanpassen van de 302 (tijdelijke omleiding) naar 301 (permanente omleiding). Daarmee was weer een klein beetje van de migratie een feit.
En, toch niet
Superblij dat alles werkte ging ik verder met pong. Snel afgeleid, he. Toen ik vanuit de Google Search Console uiteindelijk eens een keer ’echt’ op de link klikte, in plaats van copy-paste, bleek dat de link encoded was. Iedere slash en dubbele punt was vervangen door %2F en %3A. Helaas, nog een keer met ChatGPT in gesprek
Based on the rewrite rules from earlier, give me the rewrite rules when the parameters are escaped:
https://ajelsinga.nl/wp-login.php?redirect_to=https%3A%2F%2Fajelsinga.nl%2Fvideo-friday-jung-smart-house-solution%2FResultaat:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)redirect_to=https%3A%2F%2Fajelsinga.nl%2F(.*)$
RewriteRule ^wp-login\.php$ /%1? [R=302,L]Bleek er ook nog zo’n irritante slash aan het einde van de url encoded te zijn:
How do I remove the last backslash from the encoded line?
Gelukkig was daar ook een makkelijk antwoord op:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)redirect_to=https%3A%2F%2Fajelsinga.nl%2F([^%]*)%2F$
RewriteRule ^wp-login\.php$ /%1? [R=302,L]Conclusie en waarschuwing
Zoals je ziet kan ChatGPT een handig hulpmiddel zijn. Het hoeft niet om ingewikkelde vragen te gaan als ‘schrijf een businessplan voor een fictief bedrijf, waarbij minimaal de hoofdstukken inleiding, financiën, marketing in voorkomen en je een opzet opneemt voor een SWOT-analyse’, of ‘maak een kloon van het pong spelletje, geschreven in Python, zonder gebruik te maken van grafische libraries’. Voor beide voorbeelden kun je ChatGPT prima gebruiken, al zul je wel wat zaken moeten controleren en herschrijven. Je kunt het dus ook inzetten voor kleinere vragen, zoals het creëren van de juiste RewriteRules.
En hier is dan de waarschuwing. Zorg dat je voldoende van de materie weet, zodat je het antwoord van de AI kunt checken op correctheid. Vertrouw niet blind op de AI. AI’s kunnen ‘hallucineren’, waardoor ze volhardend zijn in het antwoord, terwijl het ronduit onzin is. Een tweede waarschuwing: Denk niet dat het jou niet zal raken. Het zal iedereen raken. Niet dat ik (heel) bang ben voor een Skynet-achtige toekomst , maar het zal zeker je werk veranderen. Je kunt maar beter een beetje bijblijven op dit gebied.
NB: Ik heb de code voor pong op github gezet. Het is zeker nog niet af, maar dan kun je zien wat er met AI mogelijk is.


