Forum Moderators: phranque

Message Too Old, No Replies

URL rewrite problem

Can't remove trailing string

         

nubbin

9:52 pm on Apr 17, 2017 (gmt 0)

10+ Year Member



Hi,
I'd be grateful for some help getting a url rewrite to work. The problem is part of the original URL is being left on the end of the new URL.
What I want to happen:
From URL: http://oldsite.com/product_info.php?currency=EUR&products_id=61
To URL: http://www.newsite.com/new-url-blurb-61


Problem is the To URL is coming out as:
http://www.newsite.com/new-url-blurb-61/?currency=EUR&products_id=61


Here is my code from my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_URI} product_info.php [NC]
RewriteCond %{QUERY_STRING} currency=EUR&products_id=61 [NC]
RewriteRule ^(.*)$ http://www.newsite.com/new-url-blurb-61/ [R=301,L]

</IfModule>


What am I doing wrong and how do I fix it please ?

Thanks very much


[edited by: not2easy at 10:56 pm (utc) on Apr 17, 2017]
[edit reason] readability edit [/edit]

lucy24

3:12 am on Apr 18, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^(.*)$ http://www.example.com/new-url-blurb-61/ [R=301,L]

needs to be
RewriteRule ^(.*)$ http://www.example.com/new-url-blurb-61/? [R=301,L]

The question mark means “get rid of the query string”. (Technically it means “replace the existing query string with whatever comes after the question mark”--which happens to be nothing.)

In Apache 2.4 you can also use the flag QSD (Query String Delete, duh)--but why use four bytes when one will do ;)

I assume the (.*) business is just for posting purposes.

phranque

11:38 am on Apr 18, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The question mark means “get rid of the query string”. (Technically it means “replace the existing query string with whatever comes after the question mark”--which happens to be nothing.)


Modifying the Query String:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.