0

In my httaccess file on Drupal I want every URL possibility to end up redirecting to https www site org (Note the 's' and 'www')

I can get the following working:

  1. www.site to auto redirect to https www.site
  2. http www.site to redirect to https www.site

I can't get:

  1. https site to redirect to https www.site - instead it goes to https site
  2. http site to auto redirect to https www.site - instead it goes to https site
  3. site to auto redirect to https www.site - instead it goes to https site

Here is the httaccess code so far:

 # 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.

 RewriteEngine on
  RewriteRule ^ - [E=protossl:s]
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/foo will be redirected to http://www.example.com/foo)
  # uncomment the following:
  # RewriteCond %{HTTP_HOST} .
  # RewriteCond %{HTTP_HOST} !^www\. [NC]
  # RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Thank you for your help

1 Answer 1

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

In the code you posted, the last part (commented out) looks like it should do what you require - so you simply need to uncomment those last 3 lines!?

However, you should also change the order of these directives to avoid mutliple redirects when requesting a URL of the form http://example.com.

For example:

RewriteEngine on

# 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:s]

# If your site can be accessed both with and without the 'www.' prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/foo will be redirected to http://www.example.com/foo)
# uncomment the following:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# HTTP to HTTPS redirect (By this stage the HTTP_HOST is already canonicalised)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Other...
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

As always, clear your browser cache before testing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .