Sfoglia il codice sorgente

sitemap 동적 생성 및 색인 제어 추가

기존 public/sitemap.xml 은 손으로 갱신하는 정적 파일로, 목록 페이지 14건만 담고 있고
개별 게시글이 한 건도 없었다(lastmod 도 2026-01-10 에 멈춤). 블로그에서 색인 가치가
가장 높은 게시글 본문이 전부 빠져 있어 DB 에서 직접 생성하도록 바꾼다.

- SitemapController: 고정 페이지 + 게시판 목록 + 공개 게시글 전체.
  공개 판정은 Board 모델의 기존 공개 목록 쿼리와 동일하게 맞춤
  (tb_board.is_display=1, tb_post.is_delete=0, is_secret=0, is_personal=0).
  lastmod 는 게시글의 updated_at. 1000건 단위 chunk + 1시간 캐시.
  주소는 url('/') 기준 — config('app.url') 은 환경별로 어긋날 수 있어 쓰지 않는다.
- 레이아웃(desktop/mobile app): canonical 추가. ?page= 파라미터가 붙은 주소의 중복 색인을 막는다.
- robots.txt: /editor/uploader(운영에서 200 확인), 인증/계정/API, write·edit·uploader 차단.
  /admin*/ 는 /admin/ 로 정확히 지정.
- 정적 public/sitemap.xml 제거 (웹서버가 실제 파일을 라우트보다 먼저 서빙하므로)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
KIM-JINO5 22 ore fa
parent
commit
701cbae1f6

+ 94 - 0
app/Http/Controllers/SitemapController.php

@@ -0,0 +1,94 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
+
+/**
+ * sitemap.xml 생성
+ *
+ * 기존에는 public/sitemap.xml 정적 파일을 손으로 갱신했는데, 목록 페이지 14건만 담겨 있고
+ * 개별 게시글이 한 건도 없었다(lastmod 도 2026-01-10 에 멈춰 있었다).
+ * 블로그에서 색인 가치가 가장 높은 것이 게시글 본문이므로 DB 에서 직접 생성한다.
+ *
+ * 공개 판정 조건은 Board 모델의 기존 공개 목록 쿼리와 동일하게 맞춘다.
+ * (BRD.is_display = 1, PST.is_delete = 0, PST.is_secret = 0, PST.is_personal = 0)
+ */
+class SitemapController extends Controller
+{
+    // 게시글이 늘어나도 매 요청마다 전체 조회를 하지 않도록 캐시한다.
+    private const CACHE_KEY = 'sitemap.xml';
+    private const CACHE_SECONDS = 3600;
+
+    public function index()
+    {
+        $xml = Cache::remember(self::CACHE_KEY, self::CACHE_SECONDS, function () {
+            return $this->build();
+        });
+
+        return response($xml, 200)->header('Content-Type', 'application/xml; charset=utf-8');
+    }
+
+    private function build(): string
+    {
+        // 레이아웃의 canonical(url()->current())과 같은 방식으로 요청 기준 주소를 쓴다.
+        // config('app.url') 은 환경파일 값에 좌우되어(로컬은 local-blog.web.or.kr) 어긋날 수 있다.
+        $baseUrl = rtrim(url('/'), '/');
+        $today = date('Y-m-d');
+
+        $xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
+        $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
+
+        // 고정 페이지
+        $xml .= $this->url("{$baseUrl}/", $today, 'daily', '1.0');
+        $xml .= $this->url("{$baseUrl}/recently", $today, 'daily', '0.8');
+        $xml .= $this->url("{$baseUrl}/tag", $today, 'daily', '0.6');
+
+        // 문서 페이지. tb_document 에 노출 여부 컬럼을 확인하지 못해 현재 공개된 두 건만 둔다.
+        $xml .= $this->url("{$baseUrl}/document/profile", $today, 'monthly', '0.5');
+        $xml .= $this->url("{$baseUrl}/document/proposal", $today, 'monthly', '0.5');
+
+        // 게시판 목록
+        $boards = DB::table('tb_board')
+            ->where('is_display', '=', 1)
+            ->orderBy('sort')
+            ->pluck('code');
+
+        foreach ($boards as $code) {
+            $xml .= $this->url("{$baseUrl}/board/{$code}", $today, 'daily', '0.8');
+        }
+
+        // 게시글. 공지/전체공지도 실제로 열리는 페이지이므로 함께 넣는다.
+        DB::table('tb_post')
+            ->join('tb_board', 'tb_board.id', '=', 'tb_post.board_id')
+            ->where('tb_board.is_display', '=', 1)
+            ->where('tb_post.is_delete', '=', 0)
+            ->where('tb_post.is_secret', '=', 0)
+            ->where('tb_post.is_personal', '=', 0)
+            ->select('tb_board.code', 'tb_post.id', 'tb_post.updated_at', 'tb_post.created_at')
+            ->orderBy('tb_post.id')
+            ->chunk(1000, function ($posts) use (&$xml, $baseUrl, $today) {
+                foreach ($posts as $post) {
+                    $modified = $post->updated_at ?: $post->created_at;
+                    $lastModified = $modified ? date('Y-m-d', strtotime($modified)) : $today;
+
+                    $xml .= $this->url("{$baseUrl}/board/{$post->code}/{$post->id}", $lastModified, 'weekly', '0.6');
+                }
+            });
+
+        $xml .= '</urlset>' . PHP_EOL;
+
+        return $xml;
+    }
+
+    private function url(string $location, string $lastModified, string $changeFrequency, string $priority): string
+    {
+        return '  <url>' . PHP_EOL
+            . '    <loc>' . htmlspecialchars($location, ENT_XML1) . '</loc>' . PHP_EOL
+            . '    <lastmod>' . $lastModified . '</lastmod>' . PHP_EOL
+            . '    <changefreq>' . $changeFrequency . '</changefreq>' . PHP_EOL
+            . '    <priority>' . $priority . '</priority>' . PHP_EOL
+            . '  </url>' . PHP_EOL;
+    }
+}

+ 13 - 2
public/robots.txt

@@ -1,4 +1,15 @@
 User-agent: *
 Allow: /
-Disallow: /admin*/
-Sitemap: https://blog.web.or.kr/sitemap.xml
+Disallow: /admin/
+Disallow: /auth/
+Disallow: /account/
+Disallow: /api/
+Disallow: /editor/uploader
+Disallow: /logout
+Disallow: /chat
+Disallow: /tag/posts
+Disallow: /*/write
+Disallow: /*/edit
+Disallow: /*/uploader
+
+Sitemap: https://blog.web.or.kr/sitemap.xml

+ 0 - 67
public/sitemap.xml

@@ -1,67 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
-  <url>
-    <loc>https://blog.web.or.kr/</loc>
-    <lastmod>2026-01-10</lastmod>
-    <changefreq>daily</changefreq>
-    <priority>1.0</priority>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/recently</loc>
-    <lastmod>2026-01-10</lastmod>
-    <changefreq>daily</changefreq>
-    <priority>0.9</priority>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/document/profile</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/notice</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/programming</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/portfolio</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/knowledge</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/photo</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/issue</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/tip</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/diary</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/board/guest</loc>
-    <lastmod>2026-01-10</lastmod>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/tag</loc>
-    <lastmod>2026-01-10</lastmod>
-    <changefreq>daily</changefreq>
-    <priority>0.7</priority>
-  </url>
-  <url>
-    <loc>https://blog.web.or.kr/document/proposal</loc>
-    <lastmod>2026-01-10</lastmod>
-    <changefreq>daily</changefreq>
-    <priority>0.8</priority>
-  </url>
-</urlset>

+ 3 - 0
resources/views/desktop/layouts/app.blade.php

@@ -21,6 +21,9 @@
     @if($metaDescription = config('meta_description'))
         <meta name="description" content="{{ $metaDescription }}"/>
     @endif
+    {{-- 정규 주소(canonical). ?page= 같은 파라미터가 붙은 주소가 별도 페이지로 색인되는 것을 막는다. --}}
+    <link rel="canonical" href="{{ url()->current() }}"/>
+
     {!! config('meta_adds_info') !!}
 
     <!-- CSRF Token -->

+ 3 - 0
resources/views/mobile/layouts/app.blade.php

@@ -21,6 +21,9 @@
     @if($metaDescription = config('meta_description'))
         <meta name="description" content="{{ $metaDescription }}"/>
     @endif
+    {{-- 정규 주소(canonical). ?page= 같은 파라미터가 붙은 주소가 별도 페이지로 색인되는 것을 막는다. --}}
+    <link rel="canonical" href="{{ url()->current() }}"/>
+
     {!! config('meta_adds_info') !!}
 
     <!-- CSRF Token -->

+ 3 - 0
routes/web.php

@@ -48,6 +48,9 @@ Route::get('/', [MainController::class, 'index'])->name('main');
 // 최신글 보기
 Route::get('/recently', [MainController::class, 'recently'])->name('recently');
 
+// sitemap.xml (public/sitemap.xml 정적 파일을 제거했으므로 이 라우트가 응답한다)
+Route::get('/sitemap.xml', [App\Http\Controllers\SitemapController::class, 'index'])->name('sitemap');
+
 /**
 |--------------------------------------------------------------------------
 | 사용자