Skip to main content Accessibility Feedback

How to setup dynamic server redirects in Apache with .htaccess

I recently started consolidating all of my various standalone websites into GoMakeThings.com.

It’s made managing my various stuff easier, and means that the search functionality on my site can now search across articles, podcasts, my web developer toolkit, and more.

One of the questions I’ve gotten from a few people about this move is what I’m doing with this old domains, and how this impacts my SEO.

  1. I’ll keep owning those domains forever.
  2. I’ve setup redirects on the old domains that point people to the new ones.
  3. New projects will get spun up at GoMakeThings.com instead of on their own domain.

Today, I wanted to show you how I setup those redirects.

My sites all run on an Apache server hosted by DigitalOcean. On an Apache server, an .htaccess file can be used to configure various server behaviors, including redirects.

If you want to redirect all pages on a site to one place, you can use the Redirect operator.

Redirect 301 / https://gomakethings.com/toolkit/

After Redirect, you declare, in this order…

  1. The response status code (301 means “moved permanently”)
  2. The URL path to redirect from (here, / means the root URL)
  3. The URL to redirect to

In my case, I wanted smart redirects that automatically sent folks from the old pages to the news ones, not just the high-level toolkit.

If you want to match paths, you can use Apache’s RewriteEngine.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^vanillajstoolkit.com$ [NC,OR]
  RewriteCond %{HTTP_HOST} ^www.vanillajstoolkit.com$
  RewriteRule (.*)$ https://gomakethings.com/toolkit/$1 [R=301,L]
</IfModule>
  1. First, you set the RewriteEngine to on.
  2. Next, you set the RewriteCond you want to match, as a regex. In my case, I’m matching against the root domain (both with and without www.), and I want to match the entire path after it (the $).
  3. I used two flags ([]). The NC flags tells it to ignore case when matching, and OR tells it to match either condition.
  4. The RewriteRule tells Apache where to redirect the user. In this case, to gomakethings.com/toolkit/, with the matched text appended to the end ($1).
  5. The R flag indicates the response status, and the L flag indicates that its the last rule and the RewriteEngine can stop parsing.

I’m using this on my Vanilla JS Toolkit and the Vanilla JS Podcast to redirect folks to specific tools and episodes.