ChatGPT + .htaccess: simpler site migrations

Posted on January 30, 2024 • 4 min read • 828 words
ChatGPT and generative AI are increasingly making headlines. More and more, you hear concerns that it could be harmful to employment. Two lines later you…
ChatGPT + .htaccess: simpler site migrations
Photo by A.J. Elsinga

Using ChatGPT for Practical Website Migration Challenges: Navigating .htaccess and Redirects  

ChatGPT and generative AI are increasingly making headlines. More and more, you hear concerns that it could be harmful to employment. Two lines later you read that AI is the solution for the labor market shortages. Yet another two lines claim that AI (or the people behind it?) do not strictly adhere to the legitimacy of the sources on which it is trained and can just as easily generate an ‘original’ work, including the signature of the painter or writer. These are precisely the topics I do not want to discuss.

What I do want to talk about is how you can use AI for everyday problems. In this case, how I used ChatGPT to support the migration from WordPress to Hugo.

My Problem  

In the Google Search Console, you can see which links from your website are known and regularly visited by Google. If Google visits these pages and finds them to be good, they are findable on Google.

Since a number of URLs were no longer present on the site, these needed to be redirected. I did not want to miss any traffic that was forwarded by the major search engines, since I had once written an article…

The original link I wanted to forward.

https://ajelsinga.nl/wp-login.php?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/

On Apache servers, you can relatively easily redirect URLs using a RewriteRule. You specify which part of the URL the RewriteRule should act upon and where it should be forwarded to. Easy-peasy, you’d say.

Well, in this case, it was a bit of a problem. My knowledge of RegEx is not very deep. After hours of searching, fiddling, reading documentation, and fiddling some more, I hadn’t progressed much. Various regex generators provided little solace. I managed to chop off a part of the URL, making it shorter, https://ajelsinga.nl/?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution, but I couldn’t get the part behind redirect_to to work. I tried some ‘smart’ regex tricks, but the last bit couldn’t be figured out. I was treated to various HTTP500 errors since the RewriteRule was often not valid.

ChatGPT to the Rescue  

In a moment of madness or desperation, I asked ChatGPT:

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/

To my surprise, and joy, I received an answer that I could work with. Even better, I was presented with an explanation for what each line meant.

I quickly put this into the .htaccess of the site. Hooray, it worked!

image

Suddenly, I realized that the original rule from Google Search Console had an extra piece. So I asked my buddy again:

will https://ajelsinga.nl/wp-login.php?redirect_to=https://ajelsinga.nl/video-friday-jung-smart-house-solution/ also match the rule?

Again, the response was superb. Not only did I get an answer to my question, but I even received an improved set of rules that I could copy-paste after testing them, of course.

Yes! This was the solution. Everything was redirected neatly. The last thing left to do was to change the 302 (temporary redirection) to a 301 (permanent redirection), completing another small part of the migration.

However, Not Quite  

Overjoyed that everything worked, I moved on to something else, being easily distracted. However, when I clicked the link from the Google Search Console ‘for real’, instead of copy-pasting, it turned out that the link was encoded. Every slash and colon were replaced by %2F and %3A. Alas, it was time to discuss it with ChatGPT once more.

“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%2F "

Result:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)redirect_to=https%3A%2F%2Fajelsinga.nl%2F(.*)$
RewriteRule ^wp-login\.php$ /%1? [R=302,L]

Apparently, there was also an annoying slash at the end of the URL:

“How do I remove the last backslash from the encoded line?”

Luckily, there was an easy answer for that too:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)redirect_to=https%3A%2F%2Fajelsinga.nl%2F([^%]*)%2F$
RewriteRule ^wp-login\.php$ /%1? [R=302,L]

Conclusion and Warning  

As you can see, ChatGPT can be a handy tool. It doesn’t have to be about complicated questions like ‘write a business plan for a fictitious company that includes at least introduction, finance, marketing chapters and includes a SWOT analysis setup’, or ‘create a clone of the pong game, written in Python, without using graphic libraries’. For both examples, you can use ChatGPT perfectly fine, but you will need to check and rewrite some of it. So you can also use it for smaller questions, like creating the correct RewriteRules.

And here’s the warning. Make sure you know enough about the subject so you can check the AI’s answer for correctness. Do not blindly trust the AI. AIs can ‘hallucinate’, persistently giving an answer that is outright nonsense. A second warning: Don’t think it won’t affect you. It will affect everyone. Not that I am very afraid of a Skynet-like future , but it will definitely change your job. You’d better keep up with this field.

NB: I’ve put the pong code on github . It’s certainly not finished yet, but then you can see what’s possible with AI.

See also

    Follow me