.htaccess 301 将旧 url 重定向到带有查询字符串问题的新 url [英] .htaccess 301 redirection old url to new url with querystring issue

查看:48
本文介绍了.htaccess 301 将旧 url 重定向到带有查询字符串问题的新 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 URL 下重定向到带有查询字符串的新 URL 时遇到问题.

I am facing issue with redirect below URL to new URL with query string.

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

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

我还需要将字符串 word 查询为 lowercase.

I need to also query string word into lowercase as well.

注意

  • TEST_TS 关键字是第一个查询字符串的值,必须小写.
  • 对于 product-category 关键字是固定的
  • 对于subcategory 关键字是动态的[仅字符字母a-zA-Z]
  • 对于查询字符串必须删除第二个和第三个参数
  • TEST_TS keyword is the value of the first query string and must be in lowercase.
  • For product-category keyword is fixed
  • For subcategory keyword is dynamic [only characters letters a-z or A-Z]
  • For query string must removed second and third parameter

我也在 .htaccess 中尝试了以下代码,但对我不起作用.

I have also tried below code in .htaccess as well, But not working for me.

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

推荐答案

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

Define this RewriteMap in your VirtualHost section and restart Apache:

RewriteMap lc int:tolower

然后在站点根目录 .htaccess 里面有这个代码:

Then inside site root .htaccess have this code:

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 中的值转换为小写.

${lc:%2} will convert value in %2 to lowercase.

这篇关于.htaccess 301 将旧 url 重定向到带有查询字符串问题的新 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆