请求为HTTPS .co.nz网站配置全页面301 redirect至.com版本
Got it, let's get your 301 redirects configured so every HTTPS page from www.example.co.nz lands on the corresponding www.example.com page without any 404 errors. The exact setup depends on your web server or CDN—here are the most common implementations:
Apache (using .htaccess)
If you're running Apache, edit your .htaccess file in the root directory of your www.example.co.nz site with this code:
# Enable rewrite engine RewriteEngine On RewriteBase / # Redirect all HTTPS traffic from .co.nz to .com RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^www\.example\.co\.nz$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
[NC]makes the host match case-insensitive (handles accidental capitalization)$1captures the full path of the original request (e.g.,/contactor/men) and appends it to the new domain[R=301]specifies a permanent redirect, telling search engines to update their indexes[L]stops processing further rewrite rules once this one is applied
Nginx
For Nginx, edit your site's configuration file (usually in /etc/nginx/sites-available/ or similar) and add this server block:
server { listen 443 ssl; server_name www.example.co.nz; # Keep your existing SSL certs for the .co.nz domain here ssl_certificate /path/to/your/example.co.nz.crt; ssl_certificate_key /path/to/your/example.co.nz.key; # Redirect all requests to the corresponding .com page return 301 https://www.example.com$request_uri; }
$request_uri automatically includes the full original path and query parameters, so every subpage will redirect correctly. Don't forget to reload Nginx after saving changes with sudo systemctl reload nginx.
Cloudflare (if using as CDN)
If you manage your domain through Cloudflare, setting up redirects is straightforward via page rules:
- Log into your Cloudflare dashboard and select your
example.co.nzdomain - Go to Rules > Page Rules
- Click Create Page Rule
- Set the URL match to
https://www.example.co.nz/* - Choose the Forwarding URL action, select 301 Permanent Redirect
- Set the Destination URL to
https://www.example.com/$1 - Save the rule and ensure it's enabled
Important Post-Setup Checks
- Test redirects: Use tools like
curl -I https://www.example.co.nz/contactor check your browser's DevTools (Network tab) to confirm you get a 301 status code pointing to the correct .com URL - Maintain SSL certs: Keep your
example.co.nzSSL certificate active—expired certs will cause security warnings for users before the redirect happens - Update search engines: Submit your new
www.example.comdomain to Google Search Console and use the domain migration tool to signal the change to search engines - Fix internal links: Update any internal links on your site to point directly to the .com domain to avoid unnecessary redirects
内容的提问来源于stack exchange,提问作者Abdullah Al Asif

