You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用通配符将旧站所有页面301重定向至同结构新站?

How to Set Up a Wildcard 301 Redirect for Domain Swap (Identical URL Structures)

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 (so WWW.EXAMPLE-OLD.COM also redirects correctly).
  • ^(.*)$: Captures the entire path after the domain (e.g., /articles or /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 uses http or https depending 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=tech will 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

  1. Open IIS Manager, navigate to your old site, and double-click URL Rewrite.
  2. Click Add Rule(s) and select Blank Rule.
  3. Name your rule (e.g., "Domain Swap Redirect").
  4. Under Match URL:
    • Select Regular Expressions as the pattern type.
    • Enter (.*) as the pattern (captures the full path).
  5. Under Conditions:
    • Click Add, enter {HTTP_HOST} as the input, and ^www\.example-old\.com$ as the pattern.
  6. 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/articles and http://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:// with https:// 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:19:32