diff --git a/Library/Homebrew/cmd/--prefix.rb b/Library/Homebrew/cmd/--prefix.rb
index c59830833b1a92bb8c2418105f83ba3e6dbe33da..f6e7d2ee2934c4f4e1d584e3ac92829740cc3fe2 100644
--- a/Library/Homebrew/cmd/--prefix.rb
+++ b/Library/Homebrew/cmd/--prefix.rb
@@ -11,7 +11,7 @@ module Homebrew
     if ARGV.named.empty?
       puts HOMEBREW_PREFIX
     else
-      puts ARGV.resolved_formulae.map { |f| f.opt_prefix.exist? ? f.opt_prefix : f.installed_prefix }
+      puts ARGV.resolved_formulae.map(&:installed_prefix)
     end
   end
 end
diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb
index 02d4b09a0cac43915e092d289fdc2eb214677f82..eb85f804f0f8728b1c0f01a5bfe04410327d07e5 100644
--- a/Library/Homebrew/formula.rb
+++ b/Library/Homebrew/formula.rb
@@ -198,6 +198,7 @@ class Formula
     @build = active_spec.build
     @pin = FormulaPin.new(self)
     @follow_installed_alias = true
+    @versioned_prefix = false
   end
 
   # @private
@@ -548,9 +549,16 @@ class Formula
   end
 
   # The directory in the cellar that the formula is installed to.
-  # This directory contains the formula's name and version.
+  # This directory points to {#opt_prefix} if it exists and if #{prefix} is not
+  # called from within the same formula's {#install} or {#post_install} methods.
+  # Otherwise, return the full path to the formula's versioned cellar.
   def prefix(v = pkg_version)
-    Pathname.new("#{HOMEBREW_CELLAR}/#{name}/#{v}")
+    prefix = rack/v
+    if !@versioned_prefix && prefix.directory? && Keg.new(prefix).optlinked?
+      opt_prefix
+    else
+      prefix
+    end
   end
 
   # Is the formula linked?
@@ -579,7 +587,7 @@ class Formula
   # installed versions of this software
   # @private
   def rack
-    prefix.parent
+    Pathname.new("#{HOMEBREW_CELLAR}/#{name}")
   end
 
   # All currently installed prefix directories.
@@ -994,6 +1002,7 @@ class Formula
 
   # @private
   def run_post_install
+    @versioned_prefix = true
     build = self.build
     self.build = Tab.for_formula(self)
     old_tmpdir = ENV["TMPDIR"]
@@ -1008,6 +1017,7 @@ class Formula
     ENV["TMPDIR"] = old_tmpdir
     ENV["TEMP"] = old_temp
     ENV["TMP"] = old_tmp
+    @versioned_prefix = false
   end
 
   # Tell the user about any caveats regarding this package.
@@ -1110,6 +1120,7 @@ class Formula
   # where staging is a Mktemp staging context
   # @private
   def brew
+    @versioned_prefix = true
     stage do |staging|
       staging.retain! if ARGV.keep_tmp?
       prepare_patches
@@ -1123,6 +1134,8 @@ class Formula
         cp Dir["config.log", "CMakeCache.txt"], logs
       end
     end
+  ensure
+    @versioned_prefix = false
   end
 
   # @private
@@ -1624,6 +1637,7 @@ class Formula
 
   # @private
   def run_test
+    @versioned_prefix = true
     old_home = ENV["HOME"]
     old_curl_home = ENV["CURL_HOME"]
     old_tmpdir = ENV["TMPDIR"]
@@ -1655,6 +1669,7 @@ class Formula
     ENV["TEMP"] = old_temp
     ENV["TMP"] = old_tmp
     ENV["TERM"] = old_term
+    @versioned_prefix = false
   end
 
   # @private
diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb
index 90e283c9b85dbd875e3b0f28a51db03d27934d1b..24c068460bc8afc2778a77ce2d9aca243f64eb0f 100644
--- a/Library/Homebrew/formula_installer.rb
+++ b/Library/Homebrew/formula_installer.rb
@@ -546,7 +546,7 @@ class FormulaInstaller
   def summary
     s = ""
     s << "#{Emoji.install_badge}  " if Emoji.enabled?
-    s << "#{formula.prefix}: #{formula.prefix.abv}"
+    s << "#{formula.prefix.resolved_path}: #{formula.prefix.abv}"
     s << ", built in #{pretty_duration build_time}" if build_time
     s
   end
diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb
index f3aab7f97709feeeb4616e245b9e509244728753..59fed9ec347db7413521dce327cdd4a40473f275 100644
--- a/Library/Homebrew/keg.rb
+++ b/Library/Homebrew/keg.rb
@@ -175,6 +175,7 @@ class Keg
   protected :path
 
   def initialize(path)
+    path = path.resolved_path if path.to_s.start_with?("#{HOMEBREW_PREFIX}/opt/")
     raise "#{path} is not a valid keg" unless path.parent.parent.realpath == HOMEBREW_CELLAR.realpath
     raise "#{path} is not a directory" unless path.directory?
     @path = path