Server & OS

www 없이 접속했을 때 www 붙은 주소로 바꿔주기 (.htaccess 파일 설정) [출처] www 없이 접속했을 때 www 붙은 주소로 바꿔주기 (.htaccess 파일 설정)

페이지 정보

본문

출처 : https://carrotcarrot.blog.me/40200814640


<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{HTTP_HOST} ^wishtrend.com

RewriteRule (.*) http://www.wishtrend.com/$1 [R=301,L]

</IfModule>


 


이런식으로 rewrite 모듈이 있다면 htaccess 파일에 위와 같이 입력해주면

www가 없는 주소로 접속했을 경우 www가 붙은 주소로 변경해준답니다.


 

자자, 그런데!! 여기서 끝이 아닙니다.

https 와 http 두가지 모두를 쓰는 사이트를 운영하고 계신다면

이제 문제가 생기죠.


https 로 접속했을 때도 http 도메인으로 쏴주면?

(솔루션 자체에서 http로 접속 했을 때 https로 자동 리다이렉팅을 해준다면 별로 상관이 없어요..)


만약 이런 기능에 의해서 문제가 발생할 수 있다면

이 부분도 처리를 해줘야겠지요?


http는 http로 https는 https로 말이죠.


<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{HTTPS} off

RewriteCond %{HTTP_HOST} ^wishtrend.com

RewriteRule ^(.*)$ http://www.wishtrend.com/$1 [R=301,L]

RewriteCond %{HTTPS} on

RewriteCond %{HTTP_HOST} ^wishtrend.com

RewriteRule ^(.*)$ https://www.wishtrend.com/$1 [R=301,L]

</IfModule>


이런식으로 해주면 https와 http를 구분해서 접속하게 해줄 수 있답니다.


* RewirteCond 문장은 모두 and 형태로 연결이 됩니다.

or로 구성하고 싶으시다면 문장 끝에 [or] 를 입력해주시면 됩니다.


RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^wishtrend.com


이렇게요.



자자, 그럼 여기서 팁 하나더

http로 접속했을 때 무조건 https 접속하게 하거나

https로 접속했을 때 http로 접속하게 하고 싶다면?


<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{HTTPS} off

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

</IfModule>


이런식으로도 가능 합니다. 이러면 http로 접속 했을 때 https로 접속되겠죠?

반대로


<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{HTTPS} on

RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R,L]

</IfModule>


이렇게 https 를 http로 접속하게 할 수도 있답니다.


이건 포트 번호로 구별하는 추가 팁 입니다.

80 포트(기본 포트)로 접속 했을 때 무조건 https로 변경


RewriteCond %{SERVER_PORT} ^80$

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



-------------------------------------------------------------------------------


http -> http 로


<ifmodule mod_rewrite.c>

    RewriteEngine On

    RewriteBase /

    RewriteCond %{HTTPS} off

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

</ifmodule>


www 없애기


<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteCond %{HTTPS} off

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]

    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


    RewriteCond %{HTTPS} on

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]

    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

</IfModule>





관련자료

등록된 댓글이 없습니다.
Today's proverb
당신은 당신이 생각하는 대로 살아야 합니다. (폴 발레리)