• Home
  • About
  • Archives
  • Books
  • Colophon
  • Policies
  • Resume
  • Tumblr

zanshin.net

because not enough websites start with the letter “Z”

Feed on
Posts
Comments
Tumblr
« links for 2008-01-14
MacBook Air »

Fun with .htaccess

Jan 15th, 2008 by mark

Recently I have made good use of the Apache .htaccess file to redirect and rewrite URL’s to keep visitors from getting page not found errors. Moving three domains, creating a subdomain or two, and then moving things around again, has created ample opportunity for lost visitors.

There are lots and lots of pages in the Google search results for “.htaccess,” so here are my notes, all in one place, for your reading pleasure.

Redirect visitors from an old site to a new site

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.newsite.com$ [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]

  1. RewriteEngine On is required to make all the following lines work.
  2. RewriteBase / indicates we want to work within the current directory
  3. RewriteCond %{HTTP_HOST} www.oldsite.com$ [NC] defines the condition the rule(s) should be run under. In this case we are saying the when the entered URL is not (!) www.newsite.com. The [NC] makes the condition not case sensitive.
  4. RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301] is the actual rule to be run when the above condition is met. The ^ and $ define the beginning and ending of the URL the visitor has typed. The period (.) indicates any character in a regular expression, and the splat (*) says any number of characters. The visitor is sent to www.newsite.com, and anything typed in addition to the base domain is appended to www.newsite.com via the $1. [L,R=301] says that when this rule is executed is should be the last rule run, and that the redirect should be considered permanent (301).

Redirect visitors from a subdomain to a parent domain

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} subdomain.parentdomain.com$ [NC]
RewriteRule ^(.*)$ http://parentdomain.com/$1 [L,R=301]

  1. The only difference in this example is that the condition is looking for the subdomain, and the rule states the parent domain.

In both of the examples above, it is assumed that none of the pages within the site are changing their URL format. The RewriteRule is merely appending the page specific portion of the URL to the end of the new URL.

Redirect visitors using .html links to .php links

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)\.html$ http://newdomain.com/$1\.php [L,R=301]
RewriteRule ^(.*)\.php$ http://newdomain.com/$1\.php [L,R=301]
RewriteRule ^(.*)$ http://newdomain.com/ [L,R=301]

  1. The example above not only redirects visitors from the old site to the new, it rewrites any links entered ending with .html to end with .php.
  2. Also the last rule, RewriteRule ^(.*)$ http://newdomain.com/ [L,R=301], ensures that any visits to the root domain will be properly redirected. The two preceeding rules only catch URL’s entered that have a page link specified.

Redirect visitors with old URL format to new URL format

RewriteEngine on
Redirect 301 /resume.php http://markhnichols.com/
Redirect 301 /blogs/cat_life.html http://zanshin.net/category/life/
Redirect 301 /blogs/001398.html http://zanshin.net/2007/12/29/moving/

When you change the format of the URL link (beyond just changing the link type as in the previous example) you could do it as I have above.

  1. Redirect 301 /resume.php http://newdomain.com/ redirects visitors looking for my resume at my old site to its new location at a new site entirely.
  2. Redirect 301 /blogs/cat_life.html http://zanshin.net/category/life/ redirects visitors looking for a category page using the old location (/blogs/cat_life) to its new home (http://zanshin.net/category/life/).
  3. Redirect 301 /blogs/001398.html http://zanshin.net/2007/12/29/moving/ redirects visitors using a old link to a posting (/blogs/001398.html) to its new location (http://zanshin.net/2007/12/29/moving/).

I make no guarantees about your success using my examples. These redirects work for me, your mileage may vary, void where prohibited, for external use only, and see your doctor.

Tags: apache, htaccess

Posted in nerdliness

One Response to “Fun with .htaccess”

  1. on 15 Jan 2008 at 9:44 am1My Domains » Blog Archive » Fun with .htaccess

    [...] Original post by zanshin.net [...]



  • Welcome!

    Mark H. Nichols is a software architect, martial artist, nerd, and all around good guy. He works at Kansas State University in Manhattan Kansas, and lives with his wife, two cats, a pair of 7-foot Kawai concert grand pianos, a violoncello, nearly a dozen computers, and a 2010 Honda Insight EX. You can read more about Mark, and this site, or explore the archives.
  • last 3 tweets

    • Reformatting an SD card that got viruses about 3 years ago. It's a long story. You should ask me sometime. # 1 hour ago
    • The hardest part about making dumplings is not looking in the pan for 20 minutes while they cook. #hungry #stew #dumplings # 2010/03/14
    • @feliciaday Codex? # 2010/03/13
  • Most Popluar

    • Using jQuery to Create iGoogle Style Drag-and-Drop
    • Installing PostgreSQL on Mac 10.6 (Snow Leopard)
    • geek. : nerdliness is next to godliness
    • Solfège
    • Using SSHFS, MacFUSE, and Macfusion to Access Remote Filesystems
    • Solfège Repertoire
    • Install Eclipse Ganymede on Ubuntu 9.04
    • Solfège Left Thumb Pain
    • Using CRON to Control TimeMachine Backups
  • Categories

    • diversions
    • elsewhere
    • family
    • health
    • life
    • links
    • meme
    • nerdliness
    • photography
    • random
    • relationships
    • social issues
  • Archives

    • Blogroll

      • Change.gov
      • Elfenbein Klaviermusik Notes
      • Shawn Blanc
      • Sibylle Kuder

zanshin.net © 1996 - 2010 All Rights Reserved.

Policies | Web Hosting: Blue Host