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.
Comment | File | Size | Author |
---|---|---|---|
#1 | htaccess-1436370-1.patch | 584 bytes | mjross |
Comments
Comment #1
mjross CreditAttribution: mjross commentedHere is the patch file.
Comment #2
droplet CreditAttribution: droplet commentedI 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.
Comment #3
mjross CreditAttribution: mjross commentedThe default for Apache is
http://localhost
, nothttp://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.Comment #4
basvredelingThe 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: