Apache Mod_Rewrite query string to standard url - apache

I was using a .htaccess redirect 301 to redirect a URL which then appended all the query string elements to the end, leaving me with some URLs indexed in Google as /store/product/?d=department&s=section&p=product.
I have fixed the redirect by using a RewriteRule instead which doesn't append the query strings, however I'm stuck trying to rewrite the old redirected URLs with the query strings back to the original URLs (as these are looking like two different URLs to Google now).
I have managed to get a RewriteRule to sort of work, in that /store/product/xxxxx redirects to /store/product/ as it should, it just doesn't seem to work with the whole query string of.
What I have been using is:
RewriteRule ^store/product/([a-zA-Z0-9\-_\?=&]+)$ http://www.example.com/store/product/ [NC,R=301,L]
or
RewriteRule ^store/product/\?d=department&s=section&p=product$ http://www.example.com/store/product/ [NC,R=301,L]
Hope that all makes sense!
Many thanks

RewriteCond %{QUERY_STRING} !^$
RewriteRule ^store/product/.*$ http://www.example.com/store/product/? [NC,R=301,L]

You need to specify an empty query in your substitution to not have the original one automatically appended to the new URL:
RewriteRule ^store/product/[a-zA-Z0-9\-_=&]+$ http://www.example.com/store/product/? [NC,R=301,L]
Note the ? at the end of the substitution URL.

Related

Redirecting (301) multiple urls with the same query string using .htaccess

I wonder if anyone can help. I've researched this issue but not found a post that QUITE matches it so I'm a bit lost as to how to proceed.
I have a whole bunch of URLs from an old site that each use the same query string, which is used to redirect users to the mobile version of the pages. I would like to redirect these URLs to pages on the new site so that the new URLs don't display the query string.
So:
www.old-site.com/first-page/?type=88 should redirect to www.new-site.com/first-page;
www.old-site.com/second-page/?type=88 should redirect to www.new-site.com/second-page; and
www.old-site.com/third-page/?type=88 should redirect to www.new-site.com/third-page, etc.
I've tracked down code that will redirect one URL with a query string to another URL without a query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^type=88
RewriteRule (.*)$ http://www.new-site.com? [R=301,L,NC]
but, I am not sure how to deal with all these different pages that use the same query string, but each need to end up at different destinations.
Any help that anyone can provide would be most helpful.
With kind regards,
Mark
Try :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^type=88
RewriteRule (.*)$ http://www.new-site.com%{REQUEST_URI}? [R=301,L,NC]

Redirect www to non-www loses Google gclid tracking code

I've used the .htaccess code in Generic htaccess redirect www to non-www to successfully redirect www.mysite.com to mysite.com, however in the process it strips off the gclid parameter and now AdWords isn't counting clicks correctly.
Is it possible to amend the code below so that any parameters are retained after the redirect?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
From: https://wiki.apache.org/httpd/RewriteFlags/QSA
QSA = Query String Append.
This rule appends the GET query string which results from the ReWrite rule to the initial GET query string sent by the browser. For example, take the following RewriteRule:
RewriteRule ^/product/([0-9]*)/? /product.php?product_id=$1 [QSA]
This simply makes the product_id number look like a directory to the user. Now say that I have two different views of the page, view=short and view=long. For whatever reason, I don't want to make these views look like directories by using a RewriteRule. So I want to be able to do things like:
http://example.com/product/1351283/?view=short
Let's see how QSA works. With QSA, my final rewritten URL is
http://example.com/product.php?product_id=1351283&view=short
QSA has caused the RewriteEngine to append the existing query string (view=short) to the new query string (product_id=1351283). Without QSA, the existing query string is simply replaced by the new query string:
http://example.com/product.php?product_id=1351283
If you do much scripting with reliance on GET variables, it is virtually imperative that you enable the QSA flag on all of your RewriteRules.

apache redirection is adding query string to the new url

apache mod rewrite is acting weird on me:
im trying to redirect this:
http://www.website.com/page.php?id=34
to this:
http://www.website.com/
using this in the htaccess:
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^page\.php$ "http://www.website.com" [R=301,L]
it works fine, but it also adds the query string to the redirected url:
http://www.website.com/?id=34
how do I remove the query string from the redirected url?
By default, mod_rewrite won't change the query strings, it'll keep them unchanged and pass them to whatever the new URI is. To remove them, you need to end the new URL with a question mark. You also don't need the quotation marks around it. This should work:
RewriteRule ^page\.php$ http://www.website.com? [R=301,L]
i'm not sure if it is going to work but try:
RewriteRule ^page\.php "http://www.website.com" [R=301,L]

How to create RewriteRule where the request URL has query string

I feel like I'm really close, but I can't quite get this Apache RewriteRule to work correctly.
I have a URL like http://mysite.com/product.php?view=true&ID=123 and I would like to redirect to http://mysite.com/some-page.But if the product ID=456, then I'd like to redirect it to http://mysite.com/some-other-page.
So I don't need to keep the query string for my destination URLs, but I do care what it equals in the request because that will determine where I redirect the user to. I've been struggling with trying to understand how to use the mod_rewrite %{QUERY_STRING} parameter, but I just can't get it to work.
Thanks in advance for your help!
This should help a little bit.
RewriteCond %{QUERY_STIRNG} id=456
RewriteRule .* /some-other-page? [R,L]
More about Manipulating the Query String.
Like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} =view=true&ID=123
RewriteRule ^product\.php$ /some-page? [R=301,L]
RewriteCond %{QUERY_STRING} =view=true&ID=456
RewriteRule ^product\.php$ /some-other-page? [R=301,L]
You need to match ^product.php as well as query string. I did match FULL EXACT query string (both view=true and ID=123 need to be present in EXACT order). If you need only partial match (e.g. ID=123 only), then replace =view=true&ID=123 by ID=123.
Notice ? at the end of new URL -- this is to get rid of existing query string. Without it /product.php?view=true&ID=123 will be redirected as /some-page?view=true&ID=123.
I've used 301 Permanent Redirect. You may want to change it to 302 or whatever other redirect code you think is better for you.
This is to be placed in .htaccess in in website root folder. If placed elsewhere some small tweaking may be required.

Redirect If URL Param Contains Specific Substring Value

I'm trying to use mod_rewrite to redirect users coming with a certain substring in their URL.
For example, INBOUND_XXX34_MPEG, if 34 is in the URL, I need to redirect to a different integer (i.e.: INBOUND_XXX20_MPEG)
Is this type fine tooth-combing possible with mod_rewrite?
EDIT what I have so far, but fails in testing:
RewriteEngine on
RewriteRule ^(.*=\w*)34(.*)$ $120$2
Solved it!
RewriteCond %{QUERY_STRING} linkid=(.*)34(_.*)$
RewriteRule (.*) $1?linkid=%120%2 [R=301,L]
This will preserve URI, additional query params, and target the substring index.
I'm not sure about what you really want but how about this:
RewriteRule ^(.*)INBOUND_XXX34_MPEG(.*)$ $1INBOUND_XXX20_MPEG$2
I do not know if the XXXis some can of variable thing?