Welcome to WebmasterWorld Guest from 100.26.176.182

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

http to https

trying to reduce number of 301's

     
9:11 pm on Oct 2, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 25, 2004
posts:1005
votes: 47


I have a Drupal site (apache server) that I've converted to 100% to https.
I've been using a redirection checker and have found that:

http://example.com
301 redirects to:
http://www.example.com
that 301 redirects to:
https://www.example.com

The code below is from my .htaccess file.
I was thinking that if my non-www to www 301 redirect instead redirected to https://www.example.com that I could cut out one redirect with the above scenario.

The problem is I'm in way over my head on this and don't understand the syntax used.
Is there a way where this code be altered to do what I've stated above?


# Note: The following set of directives is standard Drupal .htacess coding --

# Set "protossl" to "s" if we were accessed via [....] 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]

# Note: The following set of directives is standard Drupal .htacess coding --

# 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/... will be redirected to http://www.example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Note: "My lines" added for http to https:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

[edited by: phranque at 4:19 am (utc) on Oct 3, 2016]
[edit reason] unlinked url pattern for clarity [/edit]

10:58 pm on Oct 2, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


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


The protossl environment variable (ie. %{ENV:protossl}) results in HTTP being redirected to HTTP and HTTPS being redirected to HTTPS. That's only really useful if you have your canonical HTTP to HTTPS redirect first(*1), then that directive will correctly redirect to the appropriate scheme (without having to change the code - which is probably why it's part of the "standard" Drupal code). However, since you are only interested in redirecting to HTTPS (as you suggest) then you can simply hardcode, as you've done in the other directive:

RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 


Remember to clear your browser cache before testing this (or test with caching disabled), as the earlier 301s are likely to have been cached by the browser.

*1 - Note that you shouldn't now change the order if you make the modification above - as that would bring you back to two potential redirects (unless you hardcoded the canonical host name in the substitution).

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Note that the pattern ^(.*)$ can be reduced to ^ as in the directive above. Since you aren't using a backreference in the substitution, there is no need for a capturing group in the regex.
5:17 pm on Oct 3, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 25, 2004
posts:1005
votes: 47


Whitespace, Thanks for the reply. I think I'm squared away. Better yet I think I understand what each of the directives do now, although hopefully you'll tell me if what I've done is correct. (I've tested, I'm under the impression everything works fine.)
--
I changed my previous code:
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
with your code:
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
(So now all non-www traffic [http or https] gets converted to https://www.example.com)
--
I took the (.*)$ pattern out of my second directive, I guess just to simplify and standardize the code.
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
(With this line All http://www.example.com traffic gets converted to https.)
--
Since that covers all of my bases, there's no reason for use for the protossl variable so I commented those lines out to simplify the code.
#RewriteRule ^ - [E=protossl]
#RewriteCond %{HTTPS} on
#RewriteRule ^ - [E=protossl:s]
---
Like I said above, I'm pretty sure my testing shows all of this functions properly. If you have any comments I'd listen. Thanks again.

[edited by: phranque at 6:02 am (utc) on Oct 18, 2016]
[edit reason] unlinked url pattern for clarity [/edit]

8:45 pm on Oct 3, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


Yes, that looks OK.

there's no reason for use for the protossl variable...


Yes - in htaccess at least. However, there is a slight concern that the "protossl" environment variable is referenced elsewhere in the Drupal (PHP) code. But I doubt it.
8:56 pm on Oct 16, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 25, 2004
posts:1005
votes: 47


I've implemented the code above and it seems to work fine.
--
Also in my htaccess file I have some permanent 301 redirects, written in the form:

redirect 301 /old-page.html https://www.example.com/new-page.html

For whatever reason I have the permanent 301's in my htacess file below the code above that generically rewrites
1) All non-www traffic to https://www.example.com
2) All http://www.example.com traffic to https://www.example.com

--
Since my permanent 301 redirects redirect straight to the https form, I was thinking that if I put them above the rewrite rules that I could avoid one redirect.

Right now http://www.example.com/old-page.html
redirects to
https://www.example.com/old-page.html
then redirects
https://www.example.com/new-page.html
(A total of 2 301 redirects.)

If I could get the permanent 301's to do their redirection first, then it would only take one redirect to get
http://www.example.com/old-page.html
to the correct form
https://www.example.com/new-page.html
(A total of one 301 redirects.)

----
I've changed the order in my htaccess file (placed the 301's before the rewrite rules) but the checkers I use (one is a website, the other is a Chrome extension) still state that two 301's are occurring.
(I've changed the orders of the instructions in htaccess but the rewrites still get triggered first.)

I've cleared my browser's history (files).
I've cleared my Drupal cache (I don't see how that could be involved).

I've read through my altered htaccess file right off my server (cpanel > file manager > code editor ) it shows the permanent 301's above the rewrite rules.

What is my problem here?
Operator error on my part in clearing some cache?
Maybe rewrite rules are always run before permanent 301's are executed?

Thanks.
5:22 am on Oct 17, 2016 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4596
votes: 375


The order that modules are processed is dependent on how the server is configured. The "redirect 301" rules use Mod_Alias to work and the "RewriteRules" use Mod_Rewrite to do their work. It depends on the server's httpd.conf in what order these two separate modules work. Try changing the redirect 301 to use Mod_Rewrite as in
RewriteRule ^old-page.html$ /new-page.html/? [L,R=301]
and position the page rules before the www rules.
5:49 pm on Oct 17, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 25, 2004
posts:1005
votes: 47


Thanks not2easy. That seems to have fixed my problem.

I do think that in my situation there was an order-processed issue.
I created come permanent 301 redirection chains and placed part of them above and below the rewrite section discussed above.
The 301 redirect checker still showed that they (all of them) were processed after the rewrite rules.

I was also thinking:
I tended to think of the permanent 301 directives as a point in the htaccess file where the browser's request would then exit the file and do the instructed task (evidently that is wrong).

If that is the case, even after the permanent 301 is read the original requested URL hasn't changed (been rewritten), so as the same request proceeds on down each line the file, if it matches something it should trigger that rewrite.

Anyway, thank you both not2easy and whitespace for the help.
1:42 pm on Oct 20, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 25, 2004
posts:1005
votes: 47


I've spent quite a bit of time in my htaccess file these last few days.
Everything is working fine. I decided to use the rewrite method above for permanent redirects.

I posted this question because I wanted to complete the whole redirect process in as few steps as possible.
And I have accomplished that to some extent.
I guess my point here is I struggled with this for multiple hours when if nothing else I could have been creating content.
Considering the fact that 301's don't loose any link juice, the whole issue of trying to create a more perfect redirect chain really seems pointless and a waste of time on my part. (An opinion for anyone else who might be considering making changes.) Probably the best htaccess file for non-experts is simply the one that's easiest for you to read.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members