Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

301 Redirects Move Domain

What is a 301

Think of it like a pointer that tells the search engines where they should go. The client/browser will follow the 301 (a Permanent redirect) to it’s new location.

The advantages of doing this are many fold:

  • Help your users/visitors find what they are looking for.
  • Keep your SEO juice loss to a minimum when moving domains
  • Serve only a static redirect file while you lower your hosting costs

AWK to Parse access.log

I’m showing 9 in the example, but this works just as well on the top 200 or 2000 traffic sources.

I parsed out $7 from the access.log to remove things that were likely hackers and not customers. Sadly, the majority I was parsing out was hack attempts on my old WordPress blog.

(1:2238)# awk '{ if ($7 !~ /^\/wp/ && $7 !~ /^\/sitemap/ && $7 !~ /^\/tag/ && $7 !~ /\.php/ && $7 !~ /\.xml/ && $7 !~ /\.png/) print $7 }' /var/www/logs/access.log | sort | uniq -c | sort -nr | head -n 3
 355519 /
  77231 /old/path/to-article/
  34881 /adiffere-old/path/to-article2/

Create .htaccess in /var/www/html

My content was served from /var/www/html, as seen from apache2ctl -S, so I put the .htaccess in and made sure “AllowOverride” directive was set.

In the .htaccess file start each line with 301 Redirect . This goes above the wordpress generated info on rewrites and will take place before wordpress handles anything.

On the old domain in /var/www/html I redirected to the netlify hosted site.

Redirect 301 /old/path/to-article/ https://grimoire.jamesfraze.com/new-path/to-article/
Redirect 301 /adfferet-old/path/to-article2/ https://grimoire.jamesfraze.com/new-path/to-article2/

Let Search Engines Ingest

I could update my sitemap.xml on the old site and then feed it into the search engines to speed things up, but after a few months things will eventually be updated in all search engines.

Tell Google Console

You can also log into google console and select “Site Has Moved”.

Update Tags/Categories too

Just repeat the process for tag and categories to get a complete list of them.

awk '{ if ($7 ~ /^\/tag/) print $7 }' /var/www/logs/access.log | sort | uniq -c | sort -nr | head -n 5
   3133 /tag/featured-image/
   2674 /tag/hexdump/
   2631 /tag/games/
   2538 /tag/apache/
   2451 /tag/coding/