通过.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
.htaccessprocesses rules top-to-bottom. - Replace placeholders: Swap
old-domain.com,new-domain.com, andyour-custom-homepage-pathwith your actual values. If your new homepage is just the root of the new domain, usehttps://new-domain.com/instead. - Test it: Run
curl -I https://old-domain.comin your terminal—you should see a301 Moved Permanentlystatus code pointing to your custom new homepage. For other pages, likehttps://old-domain.com/post-1, it should redirect tohttps://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 broaderlocation /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
相关产品推荐
相关产品推荐

