꿈을 바구니에 담아 간직하다 보면!!

지금 참 힘들죠? 근데 내일은 지금보다 덜 힘들거예요

힘든 건 오늘만이 아니다. 내일도, 그리고 그 다음 날도 계속될 것이다.

PHP Tip

임대 서버 환경에서 트래픽을 줄이고 사이트 성능을 최적화하려면(그누보드)

duaidot 2025. 12. 16. 07:05

전체적으로 구조화된 .htaccess 설정이 필요합니다. 아래는 기존 설정을 기반으로, 트래픽 절감 + 보안 강화 + 캐시 최적화 + 봇 차단까지 포함한 통합 .htaccess 예시입니다.


🧩 전체 통합 .htaccess 예시

#### G5_VERSION rewrite BEGIN #####

# 디렉토리 목록 노출 방지
Options -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # 실제 파일 또는 디렉터리 존재 시 리디렉션 제외
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # 이미지 파일은 리디렉션 제외
    RewriteCond %{REQUEST_URI} \.(jpg|jpeg|png|gif|webp|svg|ico)$ [NC]
    RewriteRule ^ - [L]

    # URL 리라이트 규칙 (G5 기반)
    RewriteRule ^shop/list-([0-9a-z]+)$ shop/list.php?ca_id=$1&rewrite=1 [QSA,L]
    RewriteRule ^shop/type-([0-9a-z]+)$ shop/listtype.php?type=$1&rewrite=1 [QSA,L]
    RewriteRule ^shop/([0-9a-zA-Z_\-]+)$ shop/item.php?it_id=$1&rewrite=1 [QSA,L]
    RewriteRule ^shop/(/+)/$ shop/item.php?it_seo_title=$1&rewrite=1 [QSA,L]

    RewriteRule ^content/([0-9a-zA-Z_]+)$ bbs/content.php?co_id=$1&rewrite=1 [QSA,L]
    RewriteRule ^content/(/+)/$ bbs/content.php?co_seo_title=$1&rewrite=1 [QSA,L]

    RewriteRule ^rss/([0-9a-zA-Z_]+)$ bbs/rss.php?bo_table=$1 [QSA,L]

    RewriteRule ^([0-9a-zA-Z_]+)$ bbs/board.php?bo_table=$1&rewrite=1 [QSA,L]
    RewriteRule ^([0-9a-zA-Z_]+)/(/+)/$ bbs/board.php?bo_table=$1&wr_seo_title=$2&rewrite=1 [QSA,L]
    RewriteRule ^([0-9a-zA-Z_]+)/write$ bbs/write.php?bo_table=$1&rewrite=1 [QSA,L]
    RewriteRule ^([0-9a-zA-Z_]+)/([0-9]+)$ bbs/board.php?bo_table=$1&wr_id=$2&rewrite=1 [QSA,L]
</IfModule>

# MIME 타입 지정
AddType image/jpeg .jpg .jpeg
AddType image/png .png
AddType image/gif .gif
AddType image/webp .webp

# 확장자 없는 특정 파일에 MIME 타입 강제 지정
<FilesMatch "^imagefile$">
    ForceType image/jpeg
</FilesMatch>

# Gzip 압축 활성화
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>

# 이미지 및 정적 리소스 캐시 설정
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/png  "access plus 1 month"
    ExpiresByType image/gif  "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType text/css   "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# 외부 이미지 링크 차단 (리퍼러 기반)
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?yourdomain\.com/ [NC]
RewriteRule \.(jpg|jpeg|png|gif|webp)$ - [F,NC]

# 악성 봇 차단
BrowserMatchNoCase "AhrefsBot" bad_bot
BrowserMatchNoCase "SemrushBot" bad_bot
BrowserMatchNoCase "MJ12bot" bad_bot
Order Allow,Deny
Allow from all
Deny from env=bad_bot

# 대용량 파일 접근 제한
<FilesMatch "\.(mp4|zip|exe|iso)$">
  Order allow,deny
  Deny from all
</FilesMatch>

#### G5_VERSION rewrite END #####

📌 수정 포인트 요약

기능설명

리퍼러 차단 외부 이미지 링크로 인한 트래픽 낭비 방지
봇 차단 검색봇/스크래퍼로 인한 과도한 요청 차단
대용량 파일 제한 불필요한 다운로드로 인한 트래픽 절감
정적 리소스 캐시 이미지, CSS, JS 캐싱으로 재요청 방지
압축 Gzip으로 전송량 절감

이 설정을 .htaccess에 적용하면, 임대 서버 환경에서도 트래픽 절감 + 성능 향상 + 보안 강화 효과를 기대할 수 있어요. 도메인 이름이나 파일 경로에 따라 일부 수정이 필요할 수 있습니다.