如何用通配符将旧站所有页面301重定向至同结构新站?
Absolutely! Since your old and new sites have identical URL structures, using a wildcard-style 301 redirect to swap just the domain is totally feasible—and it’s the most efficient way to handle hundreds of pages at once. No need to set up redirects one by one! Let’s break down how to do this for the most common web servers:
Apache Server (Using .htaccess)
If your old site runs on Apache, add this rule to your root .htaccess file:
RewriteEngine On # Match the old domain (case-insensitive) RewriteCond %{HTTP_HOST} ^www\.example-old\.com$ [NC] # Redirect any path to the new domain's corresponding path RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
What each part does:
RewriteEngine On: Enables Apache's rewrite module (double-check this is enabled on your server first).[NC]: Makes the domain match case-insensitive (soWWW.EXAMPLE-OLD.COMalso redirects correctly).^(.*)$: Captures the entire path after the domain (e.g.,/articlesor/blogs/1.php).[R=301]: Specifies a permanent 301 redirect—critical for SEO, as it tells search engines to transfer ranking weight to the new URLs.[L]: Stops processing any further rewrite rules after this one.
Nginx Server
For Nginx, add this server block to your configuration file (typically in /etc/nginx/sites-available/):
server { listen 80; # Target the old domain server_name www.example-old.com; # Redirect to the new domain, preserving the full request path and parameters return 301 $scheme://www.example-new.com$request_uri; }
Quick notes:
$scheme: Automatically useshttporhttpsdepending on the original request (perfect if you use SSL).$request_uri: Preserves the entire path, query strings, and fragments from the old URL—so even pages with parameters like/blogs?tag=techwill redirect correctly.
IIS Server
If you're using IIS, you can set this up via the URL Rewrite module GUI or directly in your web.config file:
Option 1: Using IIS Manager GUI
- Open IIS Manager, navigate to your old site, and double-click URL Rewrite.
- Click Add Rule(s) and select Blank Rule.
- Name your rule (e.g., "Domain Swap Redirect").
- Under Match URL:
- Select Regular Expressions as the pattern type.
- Enter
(.*)as the pattern (captures the full path).
- Under Conditions:
- Click Add, enter
{HTTP_HOST}as the input, and^www\.example-old\.com$as the pattern.
- Click Add, enter
- Under Action:
- Select Redirect as the action type.
- Enter
http://www.example-new.com/{R:1}as the redirect URL ({R:1}refers to the captured path from the pattern). - Choose Permanent (301) as the redirect type, then save the rule.
Option 2: Directly in web.config
Add this code to your root web.config file:
<configuration> <system.webServer> <rewrite> <rules> <rule name="Domain Swap Redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^www\.example-old\.com$" /> </conditions> <action type="Redirect" url="http://www.example-new.com/{R:1}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
Important Post-Setup Checks
- Test thoroughly: Visit a few old URLs (like
http://www.example-old.com/articlesandhttp://www.example-old.com/blogs/1.php) to confirm they redirect to the correct new pages. - Clear browser cache: 301 redirects are cached by browsers, so use incognito/private mode for testing to avoid cached results.
- HTTPS adjustment: If your sites use HTTPS, replace
http://withhttps://in the redirect URLs, or use$scheme(Nginx) or{REQUEST_SCHEME}(Apache) to auto-detect the protocol. - SEO impact: 301 redirects are the gold standard for preserving search engine rankings when moving domains, so this setup will help transfer your old site's authority to the new one.
内容的提问来源于stack exchange,提问作者user2334436

