多变量HTAccess URL重写规则配置问题咨询
Hey there! Let's break down how to expand your existing .htaccess rewrite rules to cover more URL formats while keeping your current setup working smoothly.
First, let's recap your current working rule—it handles URLs like category/123/some-category and maps them to category.php?mid=123&cid=some-category (with default page=1). Now let's add support for more common URL patterns without breaking what's already working.
1. Flexible Subcategory Depth + Optional Page
Your original rule includes placeholders for up to 3 subcategories, but splitting it into explicit rules for each depth makes it easier to debug and avoids unexpected matches. Here's a refined set that handles 0 to 3 subcategories, plus an optional page number:
# Skip actual files/directories (keep this at the top) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rule 1: Main category only (e.g., category/123 or category/123/4) RewriteRule ^category/([0-9]+)/?([0-9]+)?$ category.php?mid=$1&page=$2 [NC,L,QSA] # Rule 2: Main category + 1 category slug (your original working pattern) RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/?([0-9]+)?$ category.php?mid=$1&cid=$2&page=$3 [NC,L,QSA] # Rule 3: Add one subcategory (e.g., category/123/slug/sub-slug) RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/([0-9a-zA-Zßäöü\s]+)/?([0-9]+)?$ category.php?mid=$1&cid=$2&ccid=$3&page=$4 [NC,L,QSA] # Rule 4: Add two subcategories RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/([0-9a-zA-Zßäöü\s]+)/([0-9a-zA-Z.ßäöü\s]+)/?([0-9]+)?$ category.php?mid=$1&cid=$2&ccid=$3&cccid=$4&page=$5 [NC,L,QSA] # Rule 5: Full 3-level subcategory depth RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/([0-9a-zA-Zßäöü\s]+)/([0-9a-zA-Z.ßäöü\s]+)/([0-9a-zA-Z.ßäöü\s]+)/?([0-9]+)?$ category.php?mid=$1&cid=$2&ccid=$3&cccid=$4&ccccid=$5&page=$6 [NC,L,QSA]
Key improvements here:
- Added the
QSAflag to preserve any extra query parameters (e.g., if a user adds?sort=dateto the URL, it will pass through tocategory.php). - Split into separate rules for each depth level—this prevents ambiguous matches and makes troubleshooting simpler.
- The page number stays optional at the end of every URL pattern.
2. Readable Page URLs (e.g., category/123/slug/page/2)
If you prefer a more user-friendly page format like page/2 instead of a raw number, add these rules before the ones above (specific rules need to come first to avoid conflicts):
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Page format for main category + slug RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/page/([0-9]+)/?$ category.php?mid=$1&cid=$2&page=$3 [NC,L,QSA] # Page format for main category + subcategory RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/([0-9a-zA-Zßäöü\s]+)/page/([0-9]+)/?$ category.php?mid=$1&cid=$2&ccid=$3&page=$4 [NC,L,QSA]
3. Quick Tips for Smooth Operation
- Test rules one at a time: Enable Apache's
mod_rewritelog temporarily if you run into matching issues—it will show exactly how URLs are being processed. - Keep specific rules first: Rules that match more detailed URLs (like the
page/2ones) need to come before general rules to prevent the general ones from catching them prematurely. - The
NCflag keeps rules case-insensitive, which is more user-friendly for people who type URLs in mixed case.
内容的提问来源于stack exchange,提问作者Spiek Vier

