To detect https in htaccess on Sakura server

Asked 1 years ago, Updated 1 years ago, 108 views

It may not be something to ask here.

Sakura Server introduces its own ssl and wants to be unified with https and www, so I write redirects in htaccess.

Redirect from http to https

RewriteCond%{ENV:HTTPS}!^on$
RewriteCond%{HTTP:X-Sakura-Forwarded-For}^$
RewriteRule^(.*)$https://www.sample.com/$1 [R=301,L]

And because of the Sakura server specifications, I was able to do this because I had to use RewriteCond%{HTTP:X-Sakura-Forwarded-For}^$.

However, www will result in a redirect loop.

https://sample.com
From
https://www.sample.com

Redirecting to results in a direct loop.

Normal redirect from http to https also causes loops, but RewriteCond%{HTTP:X-Sakura-Forwarded-For} does not cause loops and
Then, when www is included, it seems that the variable will be replaced in the case of https access, and false will return.

On Sakura Server

https://sample.com
From
https://www.sample.com

Please let me know what htaccess is for redirecting to

.htaccess

2022-09-30 17:11

2 Answers

If you use SNI SSL on Sakura rental server,
If you set the domain setting to "Use as Multidomain (Recommended)"
When connecting via https,
no www or wwwwwwwwwwwwwwwwwwwwwwwwwwww Both HTTP_HOST in the header return "sample.com", so
This causes the loop.

To avoid this, register
No wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww. Set each domain to
Use as a Multidomain without giving wwww (Advanced)
Specify separate folders.

The SSL certificate is valid for either "no www" or "no www" in one certificate, so set it to both.
*After registering to one side, click "Backup Private Key" to download the private key and download the SSL certificate from the contract information on the membership menu.
Register this in the other domain configuration.

.htaccess without www is as follows:

RewriteEngine On
RewriteCond%{HTTP_HOST}^sample\.com$
RewriteRule^(.*)$https://www.sample.com/$1 [R=301,L]

The .htaccess with www is as follows:

RewriteEngine On
RewriteCond%{HTTP:X-Sakura-Forwarded-For}^$
RewriteRule^(.*)$https://www.sample.com/$1 [R=301,L]

I hope the following will also be helpful.
http://qiita.com/aniya/items/664491b523fe6c717b13


2022-09-30 17:11

If the variable appears to replace and false returns for https access, you can add a rule set to redirect when X-Sakura-For is not empty and HTTP_HOST is not www.sample.com.

RewriteEngine On
RewriteCond%{ENV:HTTPS}!^on$
RewriteCond%{HTTP:X-Sakura-Forwarded-For}^$
RewriteRule^(.*)$https://www.sample.com/$1 [R=301,L]

# RewriteCond%{ENV:HTTPS}^on$
#   Do you need %{ENV:HTTPS}?
RewriteCond%{HTTP:X-Sakura-Forwarded-For}!^$
RewriteCond%{HTTP_HOST}!^www.sample.com$
RewriteRule^(.*)$https://www.sample.com/$1 [R=301,L]


2022-09-30 17:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.