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

.htaccess多参数URL重定向配置问题咨询

Multi-Level URL Redirect/Rewrite for .htaccess

Got it, let's solve this multi-level URL redirection and rewriting problem for your .htaccess file. You already have the single-level redirect working, so we'll extend that logic to handle 2nd and 3rd level parameters smoothly.

We need two sets of rules:

  1. External redirects: Send visitors from the old parameter-based URLs to the clean, hierarchical URLs (e.g., /auction/category.php?lvl_1=texnologia&lvl_2=gaming → /texnologia/gaming).
  2. Internal rewrites: Let the server quietly map the clean URLs back to your original category.php file with the correct parameters, so visitors don't see the messy query string.

Full .htaccess Configuration

# Enable mod_rewrite
RewriteEngine On

# ------------------------------
# External Redirects (Old → New)
# ------------------------------
# Redirect 3rd level: /auction/category.php?lvl_1=x&lvl_2=y&lvl_3=z → /x/y/z
RewriteCond %{QUERY_STRING} ^lvl_1=([^&]+)&lvl_2=([^&]+)&lvl_3=([^&]+)$
RewriteRule ^auction/category\.php$ /%1/%2/%3? [R=301,L]

# Redirect 2nd level: /auction/category.php?lvl_1=x&lvl_2=y → /x/y
RewriteCond %{QUERY_STRING} ^lvl_1=([^&]+)&lvl_2=([^&]+)$
RewriteRule ^auction/category\.php$ /%1/%2? [R=301,L]

# Redirect 1st level: /auction/category.php?lvl_1=x → /x
RewriteCond %{QUERY_STRING} ^lvl_1=([^&]+)$
RewriteRule ^auction/category\.php$ /%1? [R=301,L]

# ------------------------------
# Internal Rewrites (New → Old)
# ------------------------------
# Rewrite 3rd level: /x/y/z → /auction/category.php?lvl_1=x&lvl_2=y&lvl_3=z
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /auction/category.php?lvl_1=$1&lvl_2=$2&lvl_3=$3 [L]

# Rewrite 2nd level: /x/y → /auction/category.php?lvl_1=x&lvl_2=y
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /auction/category.php?lvl_1=$1&lvl_2=$2 [L]

# Rewrite 1st level: /x → /auction/category.php?lvl_1=x
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /auction/category.php?lvl_1=$1 [L]

Key Explanations

  • Order matters: We handle the most complex (3rd level) rules first, then 2nd, then 1st. This prevents simpler rules from catching more complex URLs prematurely.
  • R=301: This is a permanent redirect, which is best for SEO (tells search engines to update their indexes). If you're testing, use R=302 (temporary) instead.
  • The trailing ? in redirect rules: This strips the original query string from the redirected URL, so visitors don't see /?lvl_1=... at the end of the clean URL.
  • !-f and !-d conditions: These ensure we don't rewrite real files or directories (e.g., if you have a texnologia.html file or a texnologia folder, those won't be affected).
  • [L] flag: Stops processing further rules once this one matches, avoiding conflicts.

Just drop this into your root .htaccess file, test it out, and adjust if needed!

内容的提问来源于stack exchange,提问作者Antonis Psaroulakis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:42:07