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

请求编写.htaccess规则实现特定通配符及指定URL永久重定向

.htaccess Rules for Your Permanent Redirect Requirements

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 (so XYZ.domain.com works the same as xyz.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:

  • $0 in 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.com won'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 to R=301—this avoids browser caching issues if you need to adjust the rules later.
  • Place your .htaccess file in the root directory of the xyz.domain.com site (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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:38:45