简体   繁体   English

.htaccess基于查询字符串重定向并删除查询字符串

[英].htaccess redirect based on query string and remove query string

I'm in need to have redirect instruction to redirect the old URL with query string to new URL without query string. 我需要具有重定向指令,以将带有查询字符串的旧URL重定向到没有查询字符串的新URL。 And the new URL depends on value of query string. 并且新的URL取决于查询字符串的值。 For example: 例如:

I want: http:// www.mydomain.com/product.php?id_product=AAA 我想要:http:// www.mydomain.com/product.php?id_product=AAA

Will redirect to: 将重定向到:

http:// www.mydomain.com/product-name-for-product-number-1 http:// www.mydomain.com/product-name-for-product-number-1

And

http:// www.mydomain.com/product.php?id_product=BBB http:// www.mydomain.com/product.php?id_product=BBB

Will redirect to: 将重定向到:

http:// www.mydomain.com/this-is-different-product-name-for-product-number-2 http:// www.mydomain.com/this-is-different-product-name-for-product-number-2

Thanks in advance. 提前致谢。

you can do this type of redirection: 您可以执行以下类型的重定向:

from: http:// www.mydomain.com/product-name-for-product-number-AAA 来自:http:// www.mydomain.com/product-name-for-product-number-AAA
to: http:// www.mydomain.com/product.php?id_product=AAA&name_product=product-name 到:http:// www.mydomain.com/product.php?id_product=AAA&name_product=产品名称

from: http:// www.mydomain.com/this-is-different-product-name-for-product-number-2 来自:http:// www.mydomain.com/this-is-different-product-name-for-product-number-2
to: http:// www.mydomain.com/product.php?id_product=BBB&name_product=this-is-different-product-name 到:http:// www.mydomain.com/product.php?id_product=BBB&name_product=this-is-different-product-name

you cannot retrieve dinamically the product name from htaccess file, the only method to do this is: 您无法从htaccess文件中动态检索产品名称,执行此操作的唯一方法是:

RewriteRule ^(.+)-for-product-number-(.+)$ ^product.php?id_product=$2&name_product=$1

or you need to create a rewriterule for each product you have (you can generate the .htaccess from a script that retrieve from db all products id & name): 或者您需要为每个产品创建一个重写器(您可以通过从数据库检索所有产品ID和名称的脚本生成.htaccess):

RewriteRule ^product-name-for-product-number-1$ product.php?id_product=AAA
RewriteRule ^this-is-different-product-name-for-product-number-2$ product.php?id_product=BBB

It seems to me you cannot perform a redirection based on a query string. 在我看来,您无法基于查询字符串执行重定向。 So you would have to add [QSA] flag and redirect to a script which will be able to redirect thanks to the query string. 因此,您将必须添加[QSA]标志并重定向到脚本,该脚本将能够通过查询字符串进行重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM