program story

.htaccess는 SSL / HTTPS를 사용하여 www를 www가 아닌 ​​사이트로 리디렉션합니다.

inputbox 2021. 1. 10. 17:14
반응형

.htaccess는 SSL / HTTPS를 사용하여 www를 www가 아닌 ​​사이트로 리디렉션합니다.


단일 .htaccess파일에서 작동하는 여러 도메인이 있으며 각 도메인 에는 SSL 인증서가 있습니다.

https모든 도메인에 접두사 를 강제로 www지정하는 동시에 버전이 도메인으로 리디렉션되도록해야 no-www합니다.

아래는 내 코드입니다. 작동하지 않습니다.

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

내가 달성하고 싶은 것은 : 같은 https://www.example.com것을 리디렉션하는 것 https://example.com입니다.

내가 뭘 잘못하고 있으며 어떻게 이룰 수 있습니까?


https를 사용하는 www가 아닌 ​​www

RewriteEngine on

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

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

조건은 "if (a == a + b)"와 같기 때문에 절대 참이 아닙니다.

다음을 시도합니다.

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

이렇게하면 "www.google.com"의 "google.com"이 % 1로, 나머지는 $ 1로 캡처되며 그 후 HTTP_HOST가 www (https 포함 또는 제외)로 시작할 때 2를 결합합니다.


참조 : 아파치는 www를 비 www로 리디렉션하고 HTTP를 HTTPS로 리디렉션합니다 .

http://example.com

http://www.example.com

https://www.example.com

...에

https://example.com

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

example.com 대신 기본 URL을 www.example.com으로 지정 하려면 세 번째와 다섯 번째 줄을 변경하면됩니다.

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

적용하려면 www가 아닌HTTPS를 단일 요청으로, 당신은 당신의 htaccess로에서 다음과 같은 규칙을 사용할 수 있습니다 :

RewriteEngine on

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

이렇게하면 http://www.example.com/ 또는 https://www.example.com/ 이 ssl non-www https://example.com/리디렉션됩니다 .

내가 사용하고 R의 목적을 테스트하고 피할 브라우저 캐시에 임시 리디렉션 플래그. 리디렉션을 영구적으로 만들려면 R 플래그를 R = 301변경하십시오 .


인증서는 www 및 www가 아닌 ​​https를 모두 포함해야합니다. 일부 공급자의 인증서는 www.xxxx.yyy에 대해 둘 다 포함하지만 xxxx.yyy에 대해서는 하나만 포함합니다.

재 작성 켜기 :

RewriteEngine On

모든 http가 https를 사용하도록합니다.

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://xxx.yyy/$1 [L,R=301]

www https 만 www가 아닌 ​​https를 사용하도록합니다.

RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^www[.].+$
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [L,R=301]

www가 아닌 ​​https를 처리 할 수 ​​없습니다. 그렇지 않으면 루프가 발생합니다.

[L, R = 301]에서 :

  1. L = 규칙이 처리 된 경우 더 이상 처리하지 않습니다.
  2. R = 301 = 브라우저 / 로봇에게 영구 리디렉션을 수행하도록 지시합니다.

더 일반적인

포트 의존성이 아닌보다 일반적인 접근 방식은 다음과 같습니다.

RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [R=301,QSA]

www드롭으로 URL을 만드 십시오.

RewriteCond %{HTTPS} !on
RewriteCond %{HTTPS} !1
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [R=301,QSA]

to force any non-https url, even for those system downstream from load-balancers that drop https, use https.

Note that I have not tested the forwarded options, so would appreciate feedback on any issues with them. Those lines could be left out if your system is not behind a load-balancer.

TO HTTP_HOST or not

You can use ${HTTP_HOST} to be part of the URL in the RewriteRule, or you can use your explicit canonical domain name text (xxxx.yyy above).

Specifying the domain name explicitly ensures that no slight-of-hand character-bending means are used in the user-supplied URL to possibly trick your site into doing something it might not be prepared for, or at least ensures that the proper domain name appears in the address bar, regardless of which URL string opened the page.

It might even help convert punycode-encoded domains to show the proper unicode characters in the address bar.


This worked for me:

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

RewriteEngine On

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

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

This worked for me after much trial and error. Part one is from the user above and will capture www.xxx.yyy and send to https://xxx.yyy

Part 2 looks at entered URL and checks if HTTPS, if not, it sends to HTTPS

Done in this order, it follows logic and no error occurs.

HERE is my FULL version in side htaccess with WordPress:

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

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


# 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

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

This works for me perfectly!


To me was better hardcoding "example.com" as string, so I need less rules, even you can use it to redirect from .org to .com or similar too:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
  RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
</IfModule>

ReferenceURL : https://stackoverflow.com/questions/1478173/htaccess-redirect-www-to-non-www-with-ssl-https

반응형