Excited woman using a laptop to add a redirect rule on her website

If you click on the links in this post, we may earn money from the companies mentioned in this post, at no extra cost to you. You can read the full disclaimer here.

Sharing is caring!

Have you ever needed to add a website redirect to your website or blog? A redirect is when users are automatically forwarded from one page to another. That way, no one is stuck with a broken link or on an out-of-date page. There are a few great ways to add a redirection to a particular page or post.

But what happens if you need to redirect all the pages in a subdirectory to other pages on your site?

I was able to do this yesterday, thanks to help from Google’s Gemini Advanced!

Let me share my experience with you in case it can help you with your own blog or website.

The Redirect Issue

Yesterday I came across a situation where I wanted to redirect a subdirectory of one of my websites to the corresponding page of the website itself.

For example, let’s say I had a website called mylifestyleblog.com and a subdirectory called affiliates. In that case, every page in the subdirectory would start out with mylifestyleblog.com/affiliates, such as:

mylifestyleblog.com/affiliates/resources
mylifestyleblog.com/affiliates/contact
mylifestyleblog.com/affiiates/about-us

So if I wanted to redirect the subdirectory to the main website (also known as the root), then it would go like this:

mylifestyleblog.com/affiliates/resources redirects to mylifestyleblog.com/resources
mylifestyleblog.com/affiliates/contact redirects to mylifestyleblog.com/contact
mylifestyleblog.com/affiliates/about-us redirects to mylifestyleblog.com/about-us

Now, if it was just those pages, then there are some fairly straightforward ways you could set up a redirect. For example, if you use the Rank Math WordPress plugin, then you can use its Redirection Manager on each page or post. (You can find out more about setting up redirects on Rank Math here.)

But if you have a lot of pages on your website (which many of us do!) then that could take a lot of time (and get pretty monotonous). It would be a lot easier if you could automatically redirect everything.

Redirect Subdirectory to Root with htaccess

Since I didn’t know how to do this, I asked Gemini!

Gemini is Google’s AI assistant. And I am currently using Gemini Advanced, which uses Google’s most capable AI. One of the things you can use it for is coding, so I decided to try it out.

I entered in a quick paragraph explaining the issue and mentioned I was using Drupal and had access to cPanel. (I’ve tried both talking and typing when using Gemini – both work well.)

Gemini responded with a couple of options. The one I ended up going with was editing the .htaccess file.

NOTE: Unfamiliar with theĀ .htaccess file or need a refresher? This guide should help.

What I liked about using Gemini is that I could ask it follow up questions and get basically immediate responses. This worked really well when I wasn’t 100% sure of something. I tend to question myself – for example, I’ve rewritten this paragraph multiple times already – but I was able to keep asking Gemini questions for confirmation or clarification.

Anyway, Gemini set me up with the following code to insert inside the .htaccess file that you can use, too, if you need to redirect from a subdirectory to the root directory:

RewriteEngine on
RewriteRule ^subdirectory/(.*)$ /$1 [R=301,L]

Just change the word “subdirectory” to the name of your directory. In the hypothetical we discussed earlier, that word would be “affiliates”. So your code would look like this:

RewriteEngine on
RewriteRule ^affiliates/(.*)$ /$1 [R=301,L]

TIP: If you’re editing your .htaccess file, you should make a backup first in case you need to reverse your changes. Either copy and paste its contents into a Notepad file or download it (or both).

With Gemini’s help, I added the code to the file. Per Gemini’s advice, I placed it right above the existing Drupal rewrite rules. (It was so nice to be able to check with Gemini to make sure I was adding it to the right place in the file!)

Conclusion

If you need to add a feature or fix an issue on your website, give Gemini Advanced a try!

Whenever you use artificial intelligence, it’s possible that you’ll be given information that’s incorrect. So you’ll want to keep that in mind when using Gemini for website help (or anything!).

But in my experience yesterday, Gemini was able to come up with a solution, and quickly. It’s possible that I would have figured it out eventually, but I am all for saving time and stress when you can. Plus, I was able to ask Gemini about the changes I was making every step of the way, which helped both my comfort level and accuracy.

And you can use Gemini Advanced with Google Workspace, which makes it an even more useful tool for anyone who runs an online business.

P.S. The formatting for the code snippet in this blog entry? Gemini Advanced told me how to do that, too!