简体   繁体   中英

.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. And the new URL depends on value of query string. For example:

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

Will redirect to:

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

And

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

Thanks in advance.

you can do this type of redirection:

from: http:// www.mydomain.com/product-name-for-product-number-AAA
to: http:// www.mydomain.com/product.php?id_product=AAA&name_product=product-name

from: 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

you cannot retrieve dinamically the product name from htaccess file, the only method to do this is:

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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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