Windows系统下如何将指定Web资源请求重定向至本地自定义资源?
Absolutely, this is totally doable on Windows—there are several solid approaches depending on how granular you need the redirects to be. Let’s break them down based on your use case:
If you only need the redirects to work in your browser (and don’t want to mess with system-wide settings), browser extensions are your best bet. They’re perfect for swapping specific CSS, JS, or image files:
- Redirector (Chrome/Firefox): Install the extension, then create a new rule. For example:
- Set the "Redirect from" field to the exact remote asset URL (e.g.,
https://your-target-site.com/css/main.css) or use a regex to match a group of assets (e.g.,https://your-target-site.com/images/(.*)). - Set the "Redirect to" field to your local file path using the
file://protocol (e.g.,file:///C:/your-local-assets/css/custom-main.css).
- Set the "Redirect from" field to the exact remote asset URL (e.g.,
- Tampermonkey/Greasemonkey: Write a simple userscript to dynamically replace asset URLs on the page. Here’s a quick example:
// ==UserScript== // @name Swap Target Site Assets // @match https://your-target-site.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // Replace specific CSS const cssLink = document.querySelector('link[href="https://your-target-site.com/css/main.css"]'); if (cssLink) cssLink.href = 'file:///C:/your-local-assets/css/custom-main.css'; // Replace specific JS const jsScript = document.querySelector('script[src="https://your-target-site.com/js/app.js"]'); if (jsScript) jsScript.src = 'file:///C:/your-local-assets/js/custom-app.js'; })();
For system-wide or more complex redirects (e.g., testing how local assets behave across different browsers), proxy tools like Fiddler or Charles are ideal:
- Fiddler: Open Fiddler, go to
Rules > Customize Rules, and add redirect logic to theOnBeforeRequestfunction:static function OnBeforeRequest(oSession: Session) { // Redirect specific CSS to local file if (oSession.uriContains("https://your-target-site.com/css/main.css")) { oSession.redirectURL = "file:///C:/your-local-assets/css/custom-main.css"; } // Add more rules for JS, images, etc. here } - Charles: Use the
Map Localfeature—right-click a request in the Charles proxy log, selectMap Local, then point it to your local file. You can also set up bulk mappings for entire asset directories.
If you need to redirect an entire domain to your local machine (e.g., you’re running a local copy of the target site), edit the Windows Hosts file:
- Open Notepad as Administrator, then open
C:\Windows\System32\drivers\etc\hosts. - Add a line like:
127.0.0.1 your-target-site.com - Save the file, then flush your DNS cache with the command
ipconfig /flushdnsin Command Prompt.
Note: This is only for full domain redirects—you can’t target individual CSS/JS files with this method.
If you’re comfortable with command-line tools, you can use Windows Subsystem for Linux (WSL) to run a reverse proxy like Nginx. Configure Nginx to route specific asset requests to your local files, then set your Windows system proxy to point to your WSL instance’s IP address. This works system-wide but requires more setup.
Pick the method that fits your needs: browser extensions are great for quick, browser-specific swaps, proxy tools offer more control for debugging, and the Hosts file is perfect for full domain testing.
内容的提问来源于stack exchange,提问作者Emanuele Ciriachi

