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

通过.htaccess实现WordPress域名迁移的301自定义与通用重定向咨询

301 Redirect Setup for WordPress Domain Migration (Custom Homepage + Generic Pages)

Got it, let's get this redirect sorted out for your WordPress migration. Below are step-by-step solutions for the two most common server environments (Apache and Nginx) since those are what most WordPress sites run on.

Apache Server (Using .htaccess)

Most WordPress sites use Apache, so this is the go-to method for most people. You'll need to edit the .htaccess file in the root directory of your old domain's WordPress installation.

Step-by-Step Code

# Enable mod_rewrite (usually already enabled by WordPress)
RewriteEngine On

# Custom 301 Redirect for OLD Homepage to NEW Custom Homepage
# Replace "old-domain.com" with your actual old domain, and the new URL with your target homepage
RewriteRule ^$ https://new-domain.com/your-custom-homepage-path [R=301,L]

# Generic 301 Redirect for All Other Pages (Preserves URL Path)
# Matches both non-www and www versions of the old domain
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$
RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]

Key Notes:

  • Order matters: The homepage redirect rule must come first, since .htaccess processes rules top-to-bottom.
  • Replace placeholders: Swap old-domain.com, new-domain.com, and your-custom-homepage-path with your actual values. If your new homepage is just the root of the new domain, use https://new-domain.com/ instead.
  • Test it: Run curl -I https://old-domain.com in your terminal—you should see a 301 Moved Permanently status code pointing to your custom new homepage. For other pages, like https://old-domain.com/post-1, it should redirect to https://new-domain.com/post-1.

Nginx Server

If your site is hosted on Nginx, you'll need to update your server block configuration (usually located in /etc/nginx/sites-available/ or similar).

Step-by-Step Code

server {
    listen 80;
    listen [::]:80;
    server_name old-domain.com www.old-domain.com;

    # Custom 301 Redirect for OLD Homepage to NEW Custom Homepage
    location = / {
        return 301 https://new-domain.com/your-custom-homepage-path;
    }

    # Generic 301 Redirect for All Other Pages (Preserves URL Path)
    location / {
        return 301 https://new-domain.com$request_uri;
    }
}

Key Notes:

  • location = / is an exact match for the homepage, so it takes priority over the broader location / rule.
  • After editing the config, reload Nginx to apply changes: sudo systemctl reload nginx.
  • Same as above, replace all placeholder domains and paths with your actual values.

Critical Post-Setup Checks

  • Update WordPress Settings: After setting up redirects, log into your WordPress admin (via the new domain or old domain temporarily) and go to Settings > General to update both the "WordPress Address (URL)" and "Site Address (URL)" to your new domain. This prevents redirect loops.
  • Clear Caches: If you use a CDN, server caching, or a WordPress caching plugin, clear all caches to ensure the new redirect rules take effect immediately.
  • Verify Redirects: Use your browser's developer tools (Network tab) to check that all old URLs return a 301 status code and point to the correct new URLs.

内容的提问来源于stack exchange,提问作者Mac Ank

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:58:11