Debugging Drupal localhost redirect problem

  • Posted on: 11 April 2011
  • By: Antti

After the launch of this website I came across a problem the next time I opened up my development environment; localhost redirected my Drupal site to www.localhost.com. Upon research there seemed to be a couple reasons why this could be happening. If you are having similar issues one of these fixes might help you out.

Reason 1 - Hosts file

The hosts file maps hostnames to IP addresses (e.g. localhost to 127.0.0.1) in your operating system. The hosts file is a good place to start looking if you have some strange redirect behavior happening.

In Windows the hosts file is located at c:\windows\system32\drivers\etc. If using Windows Vista or 7 you will need to open the file as administrator in order to edit it. Your file should look a lot like this:

# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handle within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost

The important line is the following:

#       127.0.0.1       localhost

Try commenting out the line by removing the '#' or if you have any other conflicting mappings to 127.0.0.1/localhost remove them and replace with above.

Another important line (only in Windows Vista and 7) is the following which maps the new IPv6:

#       ::1             localhost

In this case if you want to see whether the IPv6 is causing troubles try commenting out the line with the '#' (like above). See here for more ways to disable IPv6.

Reason 2 - .htaccess file

The .htaccess file is a file you place on your web server that allows you to override the server's global configuration. This can include a number on things, one of them being redirecting webpages.

In my case the problem was found in the Drupal root folder .htaccess file, specifically in:

  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # uncomment the following:
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I had forgotten that I commented out the highlighted lines in order to have redirection working from non-www to www on the live site. Of course this resulted localhost redirecting to www.localhost.com which was not intended.

Conclusion

In the end if was my own doing for modifying the .htaccess file and forgetting about it. On the other hand if it happens again I'm sure I'll be quicker to resolve it.