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

通过.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=145794 or ?selectedCategories=1087). The %{lower:$1} directive converts the captured title segment to lowercase, and the trailing ? strips all query parameters. The R=301 flag sends a permanent redirect (good for SEO), and L stops 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 of R=301 while testing to avoid browser caching issues. Once you confirm everything works, switch back to R=301.
  • mod_rewrite Enabled: Ensure your Apache server has the mod_rewrite module 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 05:17:42