0Pricing
Browser Extensions Development (Chrome & Edge) · บทเรียน

การเปลี่ยนเส้นทางและเขียน URL ใหม่

ค้นพบวิธีเปลี่ยนเส้นทางคำขอเครือข่ายไปยัง URL อื่น หรือเขียนบางส่วนของ URL ใหม่ตามเกณฑ์ที่กำหนด

การเปลี่ยนเส้นทางและเขียน URL ใหม่ เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Browser Extensions Development (Chrome & Edge) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to URL Magic

Welcome! In this lesson, we'll learn how browser extensions can change where network requests go or how their URLs look.

  • Redirecting sends a request to a completely different URL.
  • Rewriting modifies parts of the original URL before it's processed.

Both are powerful tools for controlling web traffic using the `declarativeNetRequest` API.

Why Redirect or Rewrite?

Modifying URLs can serve many useful purposes:

  • Enforce HTTPS: Automatically upgrade insecure HTTP connections.
  • Fix broken links: Redirect old URLs to new ones.
  • Clean up URLs: Remove unnecessary tracking parameters.
  • Block unwanted content: Redirect specific requests to an empty page.

It helps improve security, privacy, and user experience.

The `redirect` Action Type

To redirect a network request, we use the `redirect` action type in our `declarativeNetRequest` rules. This action specifies a new URL or a pattern to generate one.

Here's a basic structure for a redirect rule:

{
  "id": 1,
  "priority": 1,
  "action": {
    "type": "redirect",
    "redirect": {
      "url": "https://new-example.com"
    }
  },
  "condition": {
    "urlFilter": "https://old-example.com/*"
  }
}

Simple URL Redirect Example

Let's say you want to redirect all requests to an old blog post URL to a new one. This rule tells the browser: 'If you see a request for old-blog.html, send it to new-blog-post.html instead.'

[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "url": "https://www.example.com/new-blog-post.html"
      }
    },
    "condition": {
      "urlFilter": "https://www.example.com/old-blog.html"
    }
  }
]

Enforcing HTTPS with Redirects

A common use case is to ensure secure connections. You can redirect all HTTP requests for a specific domain to their HTTPS equivalent. This enhances user security by encrypting data.

This rule redirects any HTTP request for example.com to its HTTPS version.

[
  {
    "id": 2,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "transform": {
          "scheme": "https"
        }
      }
    },
    "condition": {
      "urlFilter": "http://example.com/*",
      "resourceTypes": ["main_frame", "sub_frame", "script", "image", "stylesheet"]
    }
  }
]

Introducing `regexSubstitution`

Sometimes you don't want to redirect to a fixed URL, but rather modify parts of the original URL. This is where regexSubstitution comes in. It uses regular expressions to match and replace parts of the URL.

The matched parts from the regexFilter in the condition can be referenced in the substitution string using backreferences like \1, \2, etc.

Rewriting URL Paths Example

Imagine you have old URLs like /users/123 and want them to become /profile/123. You can use regexSubstitution to capture the ID and rewrite the path.

This rule captures the number after /users/ and reuses it in the /profile/ path.

[
  {
    "id": 3,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "https://www.example.com/profile/\\1"
      }
    },
    "condition": {
      "regexFilter": "^https://www.example.com/users/([0-9]+)$",
      "resourceTypes": ["main_frame"]
    }
  }
]

Modifying Query Parameters

regexSubstitution is great for cleaning up query parameters. You might want to remove a specific tracking parameter, like utm_source, from URLs to improve privacy or simplify sharing.

This rule matches URLs with ?utm_source=... and removes that parameter, keeping everything else.

[
  {
    "id": 4,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "\\1"
      }
    },
    "condition": {
      "regexFilter": "^(https?://[^?]+)\\?utm_source=[^&]*(?:&(.*))?$",
      "resourceTypes": ["main_frame"],
      "excludedInitiatorDomains": ["example.com"]
    }
  }
]

Match Patterns & Regular Expressions

Remember, `urlFilter` uses match patterns, which are simpler for basic matches. `regexFilter` requires full regular expressions and is used when you need to capture parts of the URL for `regexSubstitution`.

  • Match Patterns: Wildcards like *, specific schemes/hosts.
  • Regular Expressions: Powerful pattern matching with groups () and special characters.

Choose the right tool for the job!

Redirect Challenge

Consider a rule designed to redirect all requests from http://insecure.com to https://secure.com. Which `redirect` action type is best suited for this goal?

Recap: Redirects & Rewrites

Great job! You've learned how to powerfully control network requests:

  • Redirects: Send requests to entirely new URLs using `redirect` with a `url`.
  • Rewrites: Modify parts of URLs using `redirect` with `transform` or `regexSubstitution`.
  • HTTPS enforcement: A practical use of `transform` to upgrade connections.
  • URL cleaning: Use `regexSubstitution` to remove or change parameters.

These techniques allow you to enhance security, fix issues, and customize user browsing experiences.

คำถามที่พบบ่อย

บทเรียน “การเปลี่ยนเส้นทางและเขียน URL ใหม่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเปลี่ยนเส้นทางและเขียน URL ใหม่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเปลี่ยนเส้นทางและเขียน URL ใหม่”

ค้นพบวิธีเปลี่ยนเส้นทางคำขอเครือข่ายไปยัง URL อื่น หรือเขียนบางส่วนของ URL ใหม่ตามเกณฑ์ที่กำหนด คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การเปลี่ยนเส้นทางและเขียน URL ใหม่” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม

ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การบล็อกคำขอเครือข่าย
  2. การแก้ไขส่วนหัวคำขอ
  3. การเปลี่ยนเส้นทางและเขียน URL ใหม่
  4. กฎแบบไดนามิกและการดีบัก declarativeNetRequest
← กลับไปที่ Browser Extensions Development (Chrome & Edge)