首页 > 解决方案 > .htaccess 301 将旧 url 重定向到带有查询字符串问题的新 url

问题描述

我面临将 URL 下方重定向到带有查询字符串的新 URL 的问题。

http://www.mywebsite.co.uk/product-category/subcategory/?id=TEST_TS&fromDate=&parts=

http://www.mywebsite.co.uk/product-category/subcategory/test_ts

我还需要查询字符串单词lowercase

笔记

我也尝试过下面的代码.htaccess,但不适合我。

RewriteRule ^product-category/([a-zA-Z0-9-/+]+)/?id=(.*)$&%{QUERY_STRING}   product-category/([a-zA-Z0-9-/+]+)/{lc:$2$} [L]

标签: .htaccessredirecturl-rewriting

解决方案


RewriteMap在您的部分中定义它VirtualHost并重新启动 Apache:

RewriteMap lc int:tolower

然后在站点根目录 .htaccess 中有以下代码:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} (/product-category/subcategory)/?\?id=([^\s&]+) [NC]
RewriteRule ^ %1/${lc:%2}? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^(product-category/subcategory)/(\w-]+)/?$ $1/?id=$2&fromDate=&parts= [L,QSA,NC]

${lc:%2}将值转换%2为小写。


推荐阅读