Drupal 7's HTTP access file, .htaccess, contains some URL rewriting code for redirecting all site visitors to the domain name starting with "www.", in case they try to go to the domain name without the prefix. For instance, you might want all visitors to go to www.example.com, and never example.com, for SEO purposes.

This is achieved by uncommenting the code on lines 81-82:

  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This works fine when used on a remote server, but not on a local server that is not set up to handle the prefix. For instance, if you have a local installation of Drupal at http://localhost/my_site/, then the rewrite code will change that into an invalid URL: http://www.localhost/my_site/

This problem can be fixed by adding the following line above the RewriteRule:

  RewriteCond %{HTTP_HOST} !^localhost$ [NC]

Or, more simply, the existing RewriteCond line can be modified:

  RewriteCond %{HTTP_HOST} !^(www\.|localhost$) [NC]

I created a patch that implements the above functionality, and will upload it in a moment. I needed to create this issue to generate an issue number.

CommentFileSizeAuthor
#1 htaccess-1436370-1.patch584 bytesmjross
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mjross’s picture

Status: Active » Needs review
FileSize
584 bytes

Here is the patch file.

droplet’s picture

I think exist rewrite is correct.

Purpose of this rule is redirect a URL/DOMAIN to "www" sub-domain. www.localhost is a valid Domain/URL. Modify your own hosts file or hack htaccess when you needed.

mjross’s picture

The default for Apache is http://localhost, not http://www.localhost. It seems far easier and more logical to me to apply this patch just once, to change the RewriteCond line to work properly with local web servers, from this point forward — instead of forcing countless Drupal users to change their hosts or .htaccess files.

basvredeling’s picture

The supplied code should be added as an optional comment only. With the independent rewrite condition. It would be a most useful suggestion to exempt localhost and perhaps also subdomains.

Perhaps something like this:

# If your site can be accessed both with and without the 'www.' prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment at least one of the following RewriteConds and the RewriteRule:
#
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteCond %{HTTP_HOST} !^localhost$ [NC]
# RewriteCond %{HTTP_HOST} !^mysubdomain\. [NC]
# RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Version: 7.12 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.