Does your Drupal 7 .htaccess Redirect Correct?
Part of administrating a website is making sure the links on the site is in order, that there are no dupes or other problem. If there are, it can cause confusion both for your visitors as well as search engines. Chance is also that you end up with so called duplicated content, where you have several different URLs pointing to the same page.
When I last looked into this for Drupal Mill, I came across a few weird ones, like www.ww.drupalmill.com. They showed up in Drupal's own log, in search results and of course also in Google Analytics and Webmaster Tools. While I have no idea where they come from, I was until then assuming that the .htaccess RewriteRule Drupal 7 comes with should have taken care of that. After all, it does redirect correct from drupalmill.com to www.drupalmill.com after I uncommented it.
Multisite Support
I'm far from an expert on the commands for Apache in the .htaccess file. Plus that if I make any mistakes, it can have bad affects on the site. Thanks to great help from Kristoffer Wiklund in the Swedish #drupal-se IRC channel we quickly where able to locate the problem though.
It turned out that the changes made for Drupal 7 to make it work with multisite-installation out-of-the-box created this side-effect. The new RewriteRule now looks like this:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Basically what it means is that Apache should add www. at the beginning to every page if just drupalmill.com was used to go to the site. It will also send back a 301 to the requester saying this is a permanent redirection.
However, many web hotels have options to send all http traffic to the server no matter what is used in the host part befor the domain name. So if someone thought it was fun to use for example mehavingfun.drupalmill.com then that would work as well. Worse though is that every other internal link on the site would now also get the mehavingfun part attached to the beginning. I'm sure you can see that isn't very good.
One Rule to Catch them All
So, how do we fix this problem then? In my case I just run Drupal Mill on this installation so I don't need to support multisites. The solution for me was to simply use the old Drupal 6 RewriteRule:
RewriteCond %{HTTP_HOST} !^www\.drupalmill\.com$ [NC]
RewriteRule ^(.*)$ http://www.drupalmill.com/$1 [L,R=301]
This rule catches everything written before .drupalmill.com and redirects it to www.drupalmill.com. It also sends a 301 back so for example search engines will know that the URL they used is wrong and they can update their index with the correct one.
What about Multisite Installations?
As mentioned, I'm no expert about this (you can't be an expert about everything), but surely there must be a way of writing a rule that works better for multisite installations as well. I have opened an issue for Drupal core about this. If you know how the rule can be improved, please go there and post a comment/patch about it.
In the meantime, have a look at how your Drupal 7 .htaccess file is handling this. Maybe it needs to be fixed as well.
Comments
brad (not verified)
Fri, 01/27/2012 - 21:44
Permalink
Re: Does your Drupal 7 .htaccess Redirect Correct?
I think you should clarify that this ONLY applies to sites where all subdomains are sent to your drupal directory, which means that both DNS and the webserver itself are configured that way. I realize you imply this in your sentence about "web hotels", but it doesn't jump out... when I read this post I momentarily panicked, thinking it applied to me, and only after doing some mental gymnastics did I realize that it definitely did NOT, because neither my DNS or my webserver will point traffic towards my drupal directory.
Add new comment