.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:
- 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). - Internal rewrites: Let the server quietly map the clean URLs back to your original
category.phpfile 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, useR=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. !-fand!-dconditions: These ensure we don't rewrite real files or directories (e.g., if you have atexnologia.htmlfile or atexnologiafolder, 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
相关产品推荐
相关产品推荐

