URL 리디렉션 및 재작성
정의된 기준에 따라 네트워크 요청을 다른 URL로 리디렉션하거나 URL의 일부를 다시 작성하는 방법을 알아봅니다.
URL 리디렉션 및 재작성은(는) CoddyKit의 무료 Browser Extensions Development (Chrome & Edge) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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 리디렉션 및 재작성” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Browser Extensions Development (Chrome & Edge) 강의 전체를 잠금 해제할 수 있습니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.
“URL 리디렉션 및 재작성”에서 뭘 배우나요?
정의된 기준에 따라 네트워크 요청을 다른 URL로 리디렉션하거나 URL의 일부를 다시 작성하는 방법을 알아봅니다. 브라우저에서 직접 실행하는 실습 코드로 Browser Extensions Development (Chrome & Edge)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Browser Extensions Development (Chrome & Edge)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Browser Extensions Development (Chrome & Edge)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“URL 리디렉션 및 재작성” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Browser Extensions Development (Chrome & Edge) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Browser Extensions Development (Chrome & Edge) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 네트워크 요청 차단
- 요청 헤더 수정
- URL 리디렉션 및 재작성
- 동적 규칙과 declarativeNetRequest 디버깅