Nice programing

.htaccess를 통해 http에서 https로

nicepro 2020. 11. 22. 20:31
반응형

.htaccess를 통해 http에서 https로


기존 질문을 살펴 봤지만 실제로 저에게 맞는 것을 발견하지 못했습니다.

현재 보안 SSL 인증서로 사이트를 운영하고 있습니다. https://www.example.co.uk 에서 액세스 할 수 있습니다 . 문제는 사이트가 http://www.example.co.uk 에서도 액세스 할 수 있다는 것입니다 .-나는 이것이 가능하기를 원하지 않습니다. http에서 https로 리디렉션하려면 필요합니다.

.htaccess 파일에서 사용할 코드 한 조각을 찾았습니다.

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.co.uk [NC] 
RewriteRule ^(.*)$ https://example.co.uk/$1 [L,R=301]

사용자가 주소 표시 줄에 example.co.uk를 입력하면 제대로 작동하지만 사용자가 'www.example.co.uk'또는 ' http : //를 입력 할 수 있도록 일종의 조건문을 추가해야합니다 . www.example.co.uk '.

[OR]과 같은 것을 사용해 보았지만 결국 서버 오류가 발생합니다.

모든 도움과 제안에 감사드립니다.

건배.


다음을 시도하십시오.

 RewriteEngine On
 RewriteCond %{HTTPS} !=on
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

또한 다음과 같이 포트 번호에 따라 리디렉션 할 수도 있습니다.

 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

이렇게하면 포트 80에서 수신 된 모든 요청이 HTTPS로 리디렉션됩니다.


.htaccess 파일에 다음 코드를 추가합니다.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

웹 사이트 도메인으로 example.com 변경

URL 리디렉션 자습서는 여기에서 찾을 수 있습니다 -.htaccess 파일을 사용하여 www가 아닌 ​​것을 www로, HTTP를 HTTPS로 리디렉션


이것을 시도, 나는 그것을 사용하고 잘 작동합니다

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

이 시도:

RewriteEngine On

RewriteCond %{HTTPS} off

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

출처 : https://www.ndchost.com/wiki/apache/redirect-http-to-https

(많은 다른 코드 블록을 시도했지만이 3 개의 라이너는 완벽하게 작동했습니다)


위의 코드를 모두 시도했지만 내 웹 사이트에서 어떤 코드도 작동하지 않습니다. 그런 다음이 코드를 시도하면이 코드가 내 웹 사이트에 완벽하게 실행됩니다. htaccess에서 다음 규칙을 사용할 수 있습니다.

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

//Redirect http to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

//Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

</IfModule>

도메인 이름으로 example.com을 변경하고 영어가 좋지 않아서 죄송합니다.


리디렉션하려면 http://example.com http://www.example.com https://www.example.com 간단한 방식으로, 당신은 htaccess로에서 다음과 같은 규칙을 사용할 수 있습니다 :

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond www.%{HTTP_HOST} ^(?:www\.)?(www\..+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]

[테스트 완료]

% {REQUEST_SCHEME} 변수는 apache 2.4부터 사용할 수 있습니다.이 변수에는 요청 된 스키마 (http 또는 https)의 값이 포함됩니다. Apache 2.4에서는 다음 규칙을 사용할 수 있습니다.

RewriteEngine on


RewriteCond %{REQUEST_SCHEME} ^http$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

There are better and more secure ways to make sure that all your traffic goes over https. For example setting up two virtual hosts and redirecting all traffic from your http to your https host. Read more on this in this answer here on security.stackexchange.com.

With setting up a virtual host for redirecting you can send a 301 status (redirect permanently) so the browser understands that all the following requests should be sent to the https server where it was redirected to. Hence no further http requests will be made after the first redirect response.

You should also carefully check the given answers because with the wrong rewrite rules set you might loose the query params from your incoming requests.


If you want to redirect HTTP to HTTPS and want to add www with each URL, use the htaccess below

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

it will first redirect HTTP to HTTPS and then it will redirect to www.


For me work ONLY this variant:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Thanks https://www.reg.ru/support/hosting-i-servery/sajty-i-domeny/kak-dobavit-redirekt/redirekt-s-http-na-https (in Russian)


# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off

SetEnv DEFAULT_PHP_VERSION 7

DirectoryIndex index.cgi index.php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# RewriteCond %{HTTP_HOST} ^compasscommunity.co.uk\.com$ [NC]
# RewriteRule ^(.*)$ https://www.compasscommunity.co.uk/$1 [L,R=301]

Since this is one of the top results in the search, if you're trying to add http to https redirect on AWS beanstalk, the accepted solution will not work. You need following code instead:

RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^.*$ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

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


Perfect Code Going to HTML index :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.html" [R=301,L]

Or

Perfect Code Going to PHP index :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.php" [R=301,L]

None of these worked for me, except for this. My site was hosted in https://www.asmallorange.com

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

참고URL : https://stackoverflow.com/questions/10489895/http-to-https-through-htaccess

반응형