技术求助:使用.htaccess实现https://example.com/dev/kampaaniad/?lang=ru到https://example.com/dev/ru/kampaaniad/的重定向
Redirect Specific Query String URL to Clean Path in .htaccess
Hey there, here's the working .htaccess setup to redirect https://example.com/dev/kampaaniad/?lang=ru to https://example.com/dev/ru/kampaaniad/, and it plays nicely with your existing WordPress rewrite rules:
<IfModule mod_rewrite.c> RewriteEngine On # Custom redirect rule - place BEFORE WordPress rules! RewriteCond %{QUERY_STRING} ^lang=ru$ RewriteRule ^dev/kampaaniad/$ /dev/ru/kampaaniad/ [QSD,R=302,L] </IfModule> # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase /dev/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /dev/index.php [L] </IfModule> # END WordPress
Quick breakdown of the custom rule:
RewriteCond %{QUERY_STRING} ^lang=ru$: Ensures we only target requests where the query string is exactlylang=ru(no extra parameters attached)RewriteRule ^dev/kampaaniad/$ /dev/ru/kampaaniad/: Maps the old path to the new clean path- Flags explained:
QSD: Drops the original query string so?lang=rudoesn't appear in the redirected URLR=302: Uses a temporary redirect (change toR=301once you've confirmed it works and want a permanent redirect for SEO)L: Stops processing any further rewrite rules once this one executes
Important note: Make sure the custom redirect rule is placed before the WordPress block. WordPress's rewrite rules will catch all unmatched requests if they come first, so putting your rule above ensures it runs first.
内容的提问来源于stack exchange,提问作者Martin Abramov
相关产品推荐
相关产品推荐

