Redirecting HTTP requests to HTTPS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I have read a lot and spent a lot of hours trying to reach the right way to set a good htaccess in order to redirect an HTTP site to HTTPS.



There are a lot of tutorials on the Internet, even on Stack Overflow, but some of them seem to be outdated and don't meet good SEO practices.



The scenario is the following one:



  • My site has Cloudflare enabled

  • My site has a valid SSL on server-side

The current htaccess of my site:



RewriteEngine On
RewriteBase /
RewriteCond %HTTP_HOST ^MYSITE.cl$
RewriteRule ^(.*) http://www.MYSITE.cl/$1 [R=301]
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


and I want to change .htaccess in order to keep my good SEO rank on Google, use the HTTPS and keep the .WWW:



#OLD PART
RewriteEngine On
RewriteBase /

# Non-www to www (NEW PART)
RewriteCond %HTTP_HOST !^www.
RewriteRule (.*) https://www.%HTTP_HOST/$1 [R=301,L]

# Non-SSL to SSL (NEW PART)
RewriteCond %HTTPS off
RewriteRule (.*) https://%HTTP_HOST/$1 [R=301,L]

#OLD PART AGAIN
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


Will this work like a charm? Do you have a better idea? Does my code have any redundancy? I think that I must set Cloudflare SSL parameters to FULL SSL (STRICT). Is that right?







share|improve this question





















  • "Will this work like a charm?" Did you try? What's your experience with it so far?
    – Mast
    Apr 18 at 8:04
















up vote
4
down vote

favorite












I have read a lot and spent a lot of hours trying to reach the right way to set a good htaccess in order to redirect an HTTP site to HTTPS.



There are a lot of tutorials on the Internet, even on Stack Overflow, but some of them seem to be outdated and don't meet good SEO practices.



The scenario is the following one:



  • My site has Cloudflare enabled

  • My site has a valid SSL on server-side

The current htaccess of my site:



RewriteEngine On
RewriteBase /
RewriteCond %HTTP_HOST ^MYSITE.cl$
RewriteRule ^(.*) http://www.MYSITE.cl/$1 [R=301]
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


and I want to change .htaccess in order to keep my good SEO rank on Google, use the HTTPS and keep the .WWW:



#OLD PART
RewriteEngine On
RewriteBase /

# Non-www to www (NEW PART)
RewriteCond %HTTP_HOST !^www.
RewriteRule (.*) https://www.%HTTP_HOST/$1 [R=301,L]

# Non-SSL to SSL (NEW PART)
RewriteCond %HTTPS off
RewriteRule (.*) https://%HTTP_HOST/$1 [R=301,L]

#OLD PART AGAIN
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


Will this work like a charm? Do you have a better idea? Does my code have any redundancy? I think that I must set Cloudflare SSL parameters to FULL SSL (STRICT). Is that right?







share|improve this question





















  • "Will this work like a charm?" Did you try? What's your experience with it so far?
    – Mast
    Apr 18 at 8:04












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have read a lot and spent a lot of hours trying to reach the right way to set a good htaccess in order to redirect an HTTP site to HTTPS.



There are a lot of tutorials on the Internet, even on Stack Overflow, but some of them seem to be outdated and don't meet good SEO practices.



The scenario is the following one:



  • My site has Cloudflare enabled

  • My site has a valid SSL on server-side

The current htaccess of my site:



RewriteEngine On
RewriteBase /
RewriteCond %HTTP_HOST ^MYSITE.cl$
RewriteRule ^(.*) http://www.MYSITE.cl/$1 [R=301]
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


and I want to change .htaccess in order to keep my good SEO rank on Google, use the HTTPS and keep the .WWW:



#OLD PART
RewriteEngine On
RewriteBase /

# Non-www to www (NEW PART)
RewriteCond %HTTP_HOST !^www.
RewriteRule (.*) https://www.%HTTP_HOST/$1 [R=301,L]

# Non-SSL to SSL (NEW PART)
RewriteCond %HTTPS off
RewriteRule (.*) https://%HTTP_HOST/$1 [R=301,L]

#OLD PART AGAIN
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


Will this work like a charm? Do you have a better idea? Does my code have any redundancy? I think that I must set Cloudflare SSL parameters to FULL SSL (STRICT). Is that right?







share|improve this question













I have read a lot and spent a lot of hours trying to reach the right way to set a good htaccess in order to redirect an HTTP site to HTTPS.



There are a lot of tutorials on the Internet, even on Stack Overflow, but some of them seem to be outdated and don't meet good SEO practices.



The scenario is the following one:



  • My site has Cloudflare enabled

  • My site has a valid SSL on server-side

The current htaccess of my site:



RewriteEngine On
RewriteBase /
RewriteCond %HTTP_HOST ^MYSITE.cl$
RewriteRule ^(.*) http://www.MYSITE.cl/$1 [R=301]
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


and I want to change .htaccess in order to keep my good SEO rank on Google, use the HTTPS and keep the .WWW:



#OLD PART
RewriteEngine On
RewriteBase /

# Non-www to www (NEW PART)
RewriteCond %HTTP_HOST !^www.
RewriteRule (.*) https://www.%HTTP_HOST/$1 [R=301,L]

# Non-SSL to SSL (NEW PART)
RewriteCond %HTTPS off
RewriteRule (.*) https://%HTTP_HOST/$1 [R=301,L]

#OLD PART AGAIN
RewriteRule ^/?find$ find.php [L]
RewriteRule ^/?do$ do.php [L]
ErrorDocument 404 /404.html
Options -Indexes


Will this work like a charm? Do you have a better idea? Does my code have any redundancy? I think that I must set Cloudflare SSL parameters to FULL SSL (STRICT). Is that right?









share|improve this question












share|improve this question




share|improve this question








edited Apr 18 at 3:11









200_success

123k14142399




123k14142399









asked Apr 18 at 1:23









eplazai

211




211











  • "Will this work like a charm?" Did you try? What's your experience with it so far?
    – Mast
    Apr 18 at 8:04
















  • "Will this work like a charm?" Did you try? What's your experience with it so far?
    – Mast
    Apr 18 at 8:04















"Will this work like a charm?" Did you try? What's your experience with it so far?
– Mast
Apr 18 at 8:04




"Will this work like a charm?" Did you try? What's your experience with it so far?
– Mast
Apr 18 at 8:04










1 Answer
1






active

oldest

votes

















up vote
1
down vote













Since your actual machines are being handled by the Cloudflare based forwarding, you can let them handle the enforced SSL and www-prefixing rules instead.



However, if you do plan on the FULL SSL feature of cloudflare, you can combine the 2 separate rules into a single one:



RewriteCond %HTTP_HOST !^www. [NC,OR]
RewriteCond %HTTPS off
RewriteCond %HTTP_HOST ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%REQUEST_URI [R=301,L,NE]


Notice that I do not rely on the $1 part of the URI, as matching (and capturing) that is just redundant.




Similarly, the next 2 rules can be combined:



RewriteRule ^/?(do|find)$ /$1.php [L]


You can chose to leave it as $1.php, but I prefer giving paths from the doc-root in my rules. It is just a personal preference.




Lastly, consider providing some gap between the Options directive and the directives provided by mod-rewrite. Again, it is another personal habit of mine; but makes it clearer to maintain large rule sets later on.






share|improve this answer























  • @Gerrit0 oh damn! Didn't notice that :/ Fixed now. Thanks :)
    – hjpotter92
    Apr 22 at 17:17










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192339%2fredirecting-http-requests-to-https%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Since your actual machines are being handled by the Cloudflare based forwarding, you can let them handle the enforced SSL and www-prefixing rules instead.



However, if you do plan on the FULL SSL feature of cloudflare, you can combine the 2 separate rules into a single one:



RewriteCond %HTTP_HOST !^www. [NC,OR]
RewriteCond %HTTPS off
RewriteCond %HTTP_HOST ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%REQUEST_URI [R=301,L,NE]


Notice that I do not rely on the $1 part of the URI, as matching (and capturing) that is just redundant.




Similarly, the next 2 rules can be combined:



RewriteRule ^/?(do|find)$ /$1.php [L]


You can chose to leave it as $1.php, but I prefer giving paths from the doc-root in my rules. It is just a personal preference.




Lastly, consider providing some gap between the Options directive and the directives provided by mod-rewrite. Again, it is another personal habit of mine; but makes it clearer to maintain large rule sets later on.






share|improve this answer























  • @Gerrit0 oh damn! Didn't notice that :/ Fixed now. Thanks :)
    – hjpotter92
    Apr 22 at 17:17














up vote
1
down vote













Since your actual machines are being handled by the Cloudflare based forwarding, you can let them handle the enforced SSL and www-prefixing rules instead.



However, if you do plan on the FULL SSL feature of cloudflare, you can combine the 2 separate rules into a single one:



RewriteCond %HTTP_HOST !^www. [NC,OR]
RewriteCond %HTTPS off
RewriteCond %HTTP_HOST ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%REQUEST_URI [R=301,L,NE]


Notice that I do not rely on the $1 part of the URI, as matching (and capturing) that is just redundant.




Similarly, the next 2 rules can be combined:



RewriteRule ^/?(do|find)$ /$1.php [L]


You can chose to leave it as $1.php, but I prefer giving paths from the doc-root in my rules. It is just a personal preference.




Lastly, consider providing some gap between the Options directive and the directives provided by mod-rewrite. Again, it is another personal habit of mine; but makes it clearer to maintain large rule sets later on.






share|improve this answer























  • @Gerrit0 oh damn! Didn't notice that :/ Fixed now. Thanks :)
    – hjpotter92
    Apr 22 at 17:17












up vote
1
down vote










up vote
1
down vote









Since your actual machines are being handled by the Cloudflare based forwarding, you can let them handle the enforced SSL and www-prefixing rules instead.



However, if you do plan on the FULL SSL feature of cloudflare, you can combine the 2 separate rules into a single one:



RewriteCond %HTTP_HOST !^www. [NC,OR]
RewriteCond %HTTPS off
RewriteCond %HTTP_HOST ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%REQUEST_URI [R=301,L,NE]


Notice that I do not rely on the $1 part of the URI, as matching (and capturing) that is just redundant.




Similarly, the next 2 rules can be combined:



RewriteRule ^/?(do|find)$ /$1.php [L]


You can chose to leave it as $1.php, but I prefer giving paths from the doc-root in my rules. It is just a personal preference.




Lastly, consider providing some gap between the Options directive and the directives provided by mod-rewrite. Again, it is another personal habit of mine; but makes it clearer to maintain large rule sets later on.






share|improve this answer















Since your actual machines are being handled by the Cloudflare based forwarding, you can let them handle the enforced SSL and www-prefixing rules instead.



However, if you do plan on the FULL SSL feature of cloudflare, you can combine the 2 separate rules into a single one:



RewriteCond %HTTP_HOST !^www. [NC,OR]
RewriteCond %HTTPS off
RewriteCond %HTTP_HOST ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%REQUEST_URI [R=301,L,NE]


Notice that I do not rely on the $1 part of the URI, as matching (and capturing) that is just redundant.




Similarly, the next 2 rules can be combined:



RewriteRule ^/?(do|find)$ /$1.php [L]


You can chose to leave it as $1.php, but I prefer giving paths from the doc-root in my rules. It is just a personal preference.




Lastly, consider providing some gap between the Options directive and the directives provided by mod-rewrite. Again, it is another personal habit of mine; but makes it clearer to maintain large rule sets later on.







share|improve this answer















share|improve this answer



share|improve this answer








edited Apr 22 at 17:17


























answered Apr 22 at 16:53









hjpotter92

4,94611539




4,94611539











  • @Gerrit0 oh damn! Didn't notice that :/ Fixed now. Thanks :)
    – hjpotter92
    Apr 22 at 17:17
















  • @Gerrit0 oh damn! Didn't notice that :/ Fixed now. Thanks :)
    – hjpotter92
    Apr 22 at 17:17















@Gerrit0 oh damn! Didn't notice that :/ Fixed now. Thanks :)
– hjpotter92
Apr 22 at 17:17




@Gerrit0 oh damn! Didn't notice that :/ Fixed now. Thanks :)
– hjpotter92
Apr 22 at 17:17












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192339%2fredirecting-http-requests-to-https%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?