Apache 2.4配置URL重定向:Drupal 8站点移除URL参数
Hey there, let's get that redirect sorted out! The issue with your original RedirectMatch rule is that it doesn't handle query strings the way you think it does—here's why and how to fix it:
Why Your Original Rule Failed
RedirectMatch only matches the path portion of a URL (everything before the ?). The ?=True part of your pattern never gets matched because the ? signals the start of the query string, which isn't included in the path that RedirectMatch looks at.
The Correct Solution Using mod_rewrite
Since we need to target the query string, mod_rewrite is the right tool for the job (it's already enabled for Drupal sites, so you're good to go). Add one of these rules to your .htaccess file (place it before Drupal's default rewrite rules to avoid conflicts):
Option 1: Target the Exact URL You Mentioned
If you only need to redirect the specific URL https://example.com/course/name-course?True (or the typo'd version with an extra =), use this:
RewriteEngine On # Match URLs where the query string is exactly "True" RewriteCond %{QUERY_STRING} ^True$ # Redirect to the path without the query string (the ? at the end clears the query string) RewriteRule ^course/name-course$ https://example.com/course/name-course? [L,R=301]
Option 2: Generalize for All Similar URLs
If you have multiple URLs ending with ?True that need the same treatment, use this broader rule:
RewriteEngine On RewriteCond %{QUERY_STRING} ^True$ RewriteRule ^(.*)$ https://example.com/$1? [L,R=301]
Important Notes
- Clear Browser Cache: 301 redirects are cached permanently by browsers, so make sure to clear your cache before testing.
- Check Drupal's .htaccess: If you're adding this to Drupal's default
.htaccess, place the rules above the# Rewrite URLs of the form 'x' to the form 'index.php?q=x'section to prevent conflicts with Drupal's routing. - Verify mod_rewrite: Ensure
mod_rewriteis enabled in your Apache config (it should be if Drupal is working properly).
内容的提问来源于stack exchange,提问作者Kelvin Joysury

