Welcome to WebmasterWorld Guest from 54.162.104.113

Forum Moderators: Ocean10000 & incrediBILL & phranque

URL rewrite problem

Can't remove trailing string

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

Junior Member

10+ Year Member

joined:Mar 21, 2003
posts: 74
votes: 0


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]

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

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13823
votes: 483


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.
11:38 am on Apr 18, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10887
votes: 72


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.