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 commentedHere is the patch file.
Comment #2
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 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:
Comment #6
poker10 commentedThanks for working on this.
.htaccessis a specific file, which often contains some customizations and therefore is not always suitable to be used 1:1 on localhost. The code is commented and it is only an example/starter code. You still need to uncomment it, therefore you can make additional changes by adding the localhost exception directly on your site while uncommenting these lines.I do not think we are going to make any additional changes to the
.htaccessfile in this D7 phase (except potential security-related changes). Given that the code is still the same in D11 as well, I think it will be the best to open a new issue for D11, if you still want to change this in the further Drupal versions. Thanks!