简体   繁体   中英

mod_rewrite 301 Redirect: from old to new dynamic url

I am trying to get a mod_rewrite working correctly.

Here is the code:

RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=301$ 
RewriteRule ^index\.php$          
/blog/shop/index.php?route=product/product&product_id=301? [R=301,L]

It works, but in the end of the new URL %3f is added. Can somebody help me to get it work?

Put no-escape flag NE at the end:

RewriteRule ^index\.php$          
/blog/shop/index.php?route=product/product&product_id=301? [R=301,L,NE]

%3f is the question mark at the end of your statement. Take that out and you should be good.

RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=301$ 
RewriteRule ^index\.php$          
/blog/shop/index.php?route=product/product&product_id=301 [R=301,L]

%3f is the "?" you have at the end of your "Target URL" - just URL escaped. Is this really supposed to be there?

The %3f is the ? at the end of your replacement in percent-encoding. Just drop the ? at the end:

RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=301$ 
RewriteRule ^index\.php$ /blog/shop/index.php?route=product/product&product_id=301 [R=301,L]

You would only use a ? at the end to indicate an empty query so that the requested query does not get appended to the new URL if it does not contain a query.

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