apache redirection is adding query string to the new url - apache

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]

Related

.htaccess redirect QUERY_STRING and not propage QUERY_STRING

I would like to make a permanent redirection of
/ask?search%255Bto%255D=test
to
/question/test
I add in my .htaccess
RewriteCond %{QUERY_STRING} ^search%255Bto%255D=([a-zA-Z0-9\-]*)
RewriteRule ^ask$ /question/%1 [R=permanent,L]
With these additional lines, I get
/ask?search%255Bto%255D=test
redirect to
/question/test?search%255Bto%255D=test
My problem is than I still have the query string (?search%255Bto%255D=test) in my redirection url.
I would like to have:
/question/test
and not
/question/test?search%255Bto%255D=test
Thanks for any help
If you want to clear the query string, simply end your target URL with a question mark. Try changing your rewrite rule to:
RewriteRule ^ask$ /question/%1? [R=permanent,L]
This specifies a new, empty query string, so the old one will not be sent as part of the redirect URL.

Simple RewriteRule for first directory

This is pretty basic but I can't find a solution that works. I just need to redirect any URLs from an old directory to a new URL.
Example:
/search/whatever
to
/jobs/search
I don't need to map the whatever, I want all traffic to a URL that begins /search to get redirected (301).
Using this:
RewriteRule /search /jobs [R=301,L]
Works but I have a URL within /jobs that also now gets redirected:
/jobs/search
And that's wrong - it needs to match the start of the URL. So I tried this:
RewriteRule ^/search /jobs [R=301,L]
But that doesn't redirect at all, so I'm stuck.
Another example would be this:
RewriteRule /careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
This creates a loop as careers-at-pure is in the old and new URLs, but the following doesn't get matched and redirected:
RewriteRule ^/careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
Any suggestions?
Thanks
The leading slash is removed from the URI when they're being put through rewrite rules in htaccess files. You need to remove the leading slash from the regex, or at least make it optional:
RewriteRule ^/?search /jobs [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?

Apache Mod_Rewrite query string to standard url

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.