请求编写.htaccess规则实现特定通配符及指定URL永久重定向
Got it, let's walk through solutions for both of your redirect needs using proper Apache mod_rewrite rules (just a quick heads-up: make sure mod_rewrite is enabled on your server first—most hosts have it on by default, but it's worth double-checking).
1. Permanent Redirect for Specific Wildcard URLs to a Target Domain
First, for your wildcard redirect requirement—since you mentioned "specific wildcard URLs," I'll cover two common scenarios you might be targeting, with rules you can tweak to fit your exact use case:
Scenario A: Wildcard Subdomain Redirect
If you want any subdomain (like abc.domain.com, xyz.domain.com, etc.) to redirect to domain.com.au while keeping the full path and query string:
RewriteEngine On # Match any subdomain under domain.com (case-insensitive) RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC] # Redirect to domain.com.au, preserving the full path and query params RewriteRule ^(.*)$ https://domain.com.au/$1 [R=301,L,QSA]
Scenario B: Wildcard Path Redirect
If you need to redirect a specific path pattern (e.g., all URLs starting with domain.com/category/*) to the corresponding path on domain.com.au:
RewriteEngine On # Match paths starting with "category/" and redirect to the same path on the new domain RewriteRule ^category/(.*)$ https://domain.com.au/category/$1 [R=301,L,QSA]
Quick Notes:
R=301= permanent redirect (search engines will update their indexes to the new URL, and browsers will cache the redirect).[NC]= case-insensitive match (soXYZ.domain.comworks the same asxyz.domain.com).L= stop processing further rules once this one runs.QSA= append any query strings (like?sort=price) from the original URL to the new one.
2. Permanent Redirect for the Exact Target URL
For your specific URL: xyz.domain.com/category_product/air-freshener-refill/77/158 → domain.com.au/category_product/air-freshener-refill/77/158, here's a precise rule that ensures the correct content loads after redirect:
RewriteEngine On # Target only the xyz.domain.com subdomain RewriteCond %{HTTP_HOST} ^xyz\.domain\.com$ [NC] # Match the exact path and redirect to the same path on domain.com.au RewriteRule ^category_product/air-freshener-refill/77/158$ https://domain.com.au/$0 [R=301,L]
How this works:
$0in the target URL grabs the entire matched path from the rule, so you don't have to retype the full path manually.- This rule only triggers for that exact URL, so other pages on
xyz.domain.comwon't be affected (unless you want them to—see the bonus rule below).
Bonus: Redirect All Paths from xyz.domain.com to domain.com.au
If you actually want every request to xyz.domain.com (not just that one URL) to redirect to domain.com.au while preserving paths/params, use this instead:
RewriteEngine On RewriteCond %{HTTP_HOST} ^xyz\.domain\.com$ [NC] RewriteRule ^(.*)$ https://domain.com.au/$1 [R=301,L,QSA]
Pro Tips:
- Test with
R=302(temporary redirect) first before switching toR=301—this avoids browser caching issues if you need to adjust the rules later. - Place your
.htaccessfile in the root directory of thexyz.domain.comsite (or the directory where the wildcard URLs are hosted). - If you're unsure if the rules work, use an online .htaccess tester tool (just make sure it's a reputable one) or test directly on your server with a browser's dev tools to check the redirect status code.
内容的提问来源于stack exchange,提问作者Jagdish Chandra Sharma

