通过.htaccess实现动态URL重写:移除查询参数并统一路径大小写的技术咨询
Solution for URL Normalization (Lowercase Title + Remove Query Params) via .htaccess
Absolutely! You can handle both the lowercase conversion of the recipe title in the URL path and the removal of all query string parameters using Apache's mod_rewrite in your .htaccess file. Since this is a bespoke website, we can craft rules tailored exactly to your URL structure.
Step-by-Step .htaccess Rules
First, make sure mod_rewrite is enabled, then add these rules:
# Enable rewrite engine RewriteEngine On # 1. Redirect URLs with query params + mixed-case titles to lowercase title (no query params) RewriteCond %{REQUEST_URI} ^/webshop/recipe/([A-Z][a-zA-Z-]+)/([0-9]+)$ RewriteCond %{QUERY_STRING} .+ RewriteRule ^webshop/recipe/([A-Za-z-]+)/([0-9]+)$ /webshop/recipe/%{lower:$1}/$2? [R=301,L] # 2. Redirect mixed-case titles (without query params) to lowercase version RewriteCond %{REQUEST_URI} ^/webshop/recipe/([A-Z][a-zA-Z-]+)/([0-9]+)$ RewriteRule ^webshop/recipe/([A-Za-z-]+)/([0-9]+)$ /webshop/recipe/%{lower:$1}/$2 [R=301,L]
What Each Rule Does:
- Rule 1: Targets URLs that have both a mixed-case recipe title (e.g.,
Actimel-Breakfast-Lollies) and any query string (like?recipeId=145794or?selectedCategories=1087). The%{lower:$1}directive converts the captured title segment to lowercase, and the trailing?strips all query parameters. TheR=301flag sends a permanent redirect (good for SEO), andLstops processing further rules for this request. - Rule 2: Catches any URLs where the title is mixed-case but there are no query params—ensuring those also get normalized to the lowercase version.
Important Notes:
- Test First: Use
R=302(temporary redirect) instead ofR=301while testing to avoid browser caching issues. Once you confirm everything works, switch back toR=301. - mod_rewrite Enabled: Ensure your Apache server has the
mod_rewritemodule enabled. If you're unsure, check with your hosting provider or server admin. - Bespoke Fit: These rules are customized for your specific URL pattern (
/webshop/recipe/[title]/[numeric-id]). If your site's URL structure changes later, you'll need to adjust the regex patterns accordingly.
内容的提问来源于stack exchange,提问作者Simon Birch
相关产品推荐
相关产品推荐

