[apache] .htaccess 리디렉션 http를 https로

이전 URL ( www1.test.net) https://www1.test.net
이 있는데 내 사이트에 SSL 인증서를 구현하고 설치 한 것으로 리디렉션하고 싶습니다 .
이것은 내 이전 파일입니다 .htaccess.

RewriteEngine On
RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]

.htaccessURL이 자동으로 리디렉션되도록 파일을 구성 하려면 https어떻게 해야 합니까?
감사!



답변

2016 업데이트

이 답변이 주목을 받으면서 가상 호스트를 사용하여 더 권장되는 방법을 힌트하고 싶습니다. Apache : Redirect SSL

<VirtualHost *:80>
   ServerName mysite.example.com
   Redirect permanent / https://mysite.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

이전 답변,
ssl-port가 80으로 설정되어 있지 않다는 점을 감안할 때 이것은 작동합니다.

RewriteEngine on

# force ssl
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

이것이 첫 번째 재 작성 규칙이어야합니다.

편집 : 이 코드는 다음을 수행합니다. RewriteCond (ition)은 요청의 ServerPort가 80인지 확인합니다 (기본 http-port입니다. 다른 포트를 지정한 경우 조건을 조정해야합니다). 그렇다면 전체 URL을 일치시키고 (.*)https-url로 리디렉션합니다. %{SERVER_NAME}특정 URL로 대체 될 수 있지만 이렇게하면 다른 프로젝트의 코드를 변경할 필요가 없습니다. %{REQUEST_URI}은 TLD (최상위 도메인) 뒤의 URL 부분이므로 사용자는 원래 위치로 리디렉션되지만 https로 리디렉션됩니다.


답변

다음을 사용하여 내 도메인의 모든 페이지를 http에서 https로 성공적으로 리디렉션합니다.

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

리디렉션을 사용하여 301 'permanently moved'리디렉션되므로 SEO 순위를 전송하는 데 도움이됩니다.

302 'temporarily moved'변경 사항을 사용하여 리디렉션하려면[R=302,L]


답변

이것은 wwwHTTPS, 프록시 및 프록시 사용자가없는 경우에 가장 적합 합니다.

RewriteEngine On

### WWW & HTTPS

# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

### WWW & HTTPS


답변

다음 코드로 https를 강제합니다.

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


답변

HTTPS / SSL 연결이로드 밸런서에서 종료되고 모든 트래픽이 포트 80의 인스턴스로 전송되는 경우 다음 규칙이 작동하여 비보안 트래픽을 리디렉션합니다.

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

mod_rewrite모듈이로드 되었는지 확인하십시오 .


답변

리디렉션하는 가장 좋은 방법을 찾고있는 중입니다 (html5boilerplate에서 제공됨).

# ----------------------------------------------------------------------
# | HTTP Strict Transport Security (HSTS)                              |
# ----------------------------------------------------------------------

# Force client-side SSL redirection.
#
# If a user types `example.com` in their browser, even if the server
# redirects them to the secure version of the website, that still leaves
# a window of opportunity (the initial HTTP connection) for an attacker
# to downgrade or redirect the request.
#
# The following header ensures that browser will ONLY connect to your
# server via HTTPS, regardless of what the users type in the browser's
# address bar.
#
# (!) Remove the `includeSubDomains` optional directive if the website's
# subdomains are not using HTTPS.
#
# http://www.html5rocks.com/en/tutorials/security/transport-layer-security/
# https://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec-14#section-6.1
# http://blogs.msdn.com/b/ieinternals/archive/2014/08/18/hsts-strict-transport-security-attacks-mitigations-deployment-https.aspx

Header set Strict-Transport-Security "max-age=16070400; includeSubDomains"

아마도 2017 년에 누군가를 도울 것입니다! 🙂


답변

.htaccess 파일에이 코드를 삽입하십시오. 그리고 그것은 작동합니다

RewriteCond %{HTTP_HOST} yourDomainName\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourDomainName.com/$1 [R,L]