.htaccess - .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
。
笔记
TEST_TS
关键字是第一个查询字符串的值,必须小写。- 因为
product-category
关键字是固定的 - 对于
subcategory
关键字是动态的[只有字符字母a-z
或A-Z
] - 对于查询字符串,必须删除第二个和第三个参数
我也尝试过下面的代码.htaccess
,但不适合我。
RewriteRule ^product-category/([a-zA-Z0-9-/+]+)/?id=(.*)$&%{QUERY_STRING} product-category/([a-zA-Z0-9-/+]+)/{lc:$2$} [L]
解决方案
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
为小写。
推荐阅读
- php - 如何加入“IN”结果并检查“where” active = 1?
- api - CodeIgniter,在 $this- 响应之后调用另一个函数
- html - 是否可以在 HTML 中使用 data-* 属性来设置 CSS 中的宽度?
- c - 忽略使用 sprintf 显示数字
- apache - 匹配查询的 Apache 重写条件
- php - "error":true,"message":"required parameters are not available" while check api call using postman
- bash - 使用 Bash 在目录中查找最近的文件
- python - Scrapy 没有正确通过 url
- reactjs - 我正在尝试设置 react-select 值而不是为每个选择元素使用状态我试图在数组中获取所有这些值
- javascript - Express.js:如何刷新后端的数据库更改?