Hi,

I normally like to redirect non-www url entries to the www version on my sites.

So if someone enters example.com up pops www.example.com

I can see where to modify drupal's .htaccess file, but it seems like the code to create this redirect is very different from the past. I am no .htaccess expert and I am struggling to understand the code in this file.

On various others sites, drupal and non-drupal the following code has done this for me:

---
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

---

Drupal now uses this in the .htaccess file:

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

===

Can anyone explain the consequences of uses this presumably "newer" method?

In trying to investigate and understand these .htaccess settings I even come across another method of apparently directing non-www traffic to www pages here:

___
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%1/$1 [R=301,L]

___

I'm confused about this.

Can anyone help? I don't want to blindly go with what drupal is suggesting in the current D7 .htaccess file without at least trying to understand why the code seems to have changed so much.

Thanks,

Sam

Comments

VM’s picture

the only significant difference is: {ENV:protossl}

The explaination for that flag is higher in the core .htaccess file

# Set "protossl" to "s" if we were accessed via https://.  This is used later
# if you enable "www." stripping or enforcement, in order to ensure that
# you don't bounce between http and https.
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]
Steady’s picture

Ok, thanks for the reply.

However, I'm afraid it doesn't help me understand what's going on.

I am really struggling to understand various drupal settings such as this newer .htaccess file.

I can't find any explanation of these changes anywhere on drupal.org and I've failed to find anything to help explain them on google either.

Can you or anyone expand on the answer I received?

Thanks,

Sam

Stefan Lehmann’s picture

The code change only takes into account if the website was originally requested via HTTPS and if so it saves you an additional redirect which might be triggered by Drupal, if the page you're looking at requests it.

Before the code change this here would have happened:
REQUEST: https://example.com
REDIRECT: http://www.example.com

Now:
REQUEST: https://example.com
REDIRECT: https://www.example.com

Does this make sense?

I like cookies!

Steady’s picture

Thank you. This does help clear things up.

Jaypan’s picture

Why is this an issue? The code is there for you to use. Tens, if not hundreds of thousands of sites are using Drupal 7, and many of them are using this code. If it didn't work, there would be issue queues on the matter opened up on it. If you don't trust it, don't use it.

Steady’s picture

Thanks for your reply.

It is not an issue per se.

However I have had problems with .htaccess yesterday, especially as the site has some static pages which need to be viewable and they also currently have a wordpress blog on part of it, which will be moved over, as well as lots of drupal pages.

Some funny things were happening, and some pages were https.

So I was trying to better understand the new htaccess page, especially as on the face of it, there have been quite a few changes.

I searched the forum, searched drupal, searched google and could find very little about the new one.

Maybe little was found because it works well.

However I was hoping there would be something on it somewhere, but that may be beyond the requirements and scope of the drupal development teams various valiant efforts.

So if this forum isn't the place to try and seek a better understanding of drupal then I humbly apologise.

I am certainly on the low end of the learning curve and I am trying my best.

Sam

VM’s picture

However I have had problems with .htaccess yesterday, especially as the site has some static pages which need to be viewable and they also currently have a wordpress blog on part of it, which will be moved over, as well as lots of drupal pages.

Some funny things were happening, and some pages were https.

Rather than ask questions regarding changes to apache directives contained in .htaccess, why not elaborate on the issues you are experiencing?