.htaccess URL rewriting for "www" breaks on localhost

Created on 10 February 2012, almost 13 years ago
Updated 7 June 2024, 7 months ago

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.

Feature request
Status

Closed: won't fix

Version

7.0 ⚰️

Component
Base 

Last updated about 10 hours ago

Created by

🇺🇸United States mjross

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇸🇰Slovakia poker10

    Thanks for working on this.

    .htaccess is 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 .htaccess file 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!

Production build 0.71.5 2024