.htaccess: Check if query string has a certain value or else redirect it - apache

I'm trying to learn a bit of .htaccess and am really anxious on what it can do. I saw a snippet online but can't get it to work, it basically goes like this If a query string does not have a certain value, redirect it to index.php instead or some other page. How do I do that?
This looks for the value apples:
www.domain.com/somefile.php?a=apples&b=xyz
This will redirect to index.php instead:
www.domain.com/somefile.php?a=stinkycheese&b=xyz

You need to use the %{QUERY_STRING} variable inside a RewriteCond. So for example, if you don't want to redirect if there exists a redirect=no in the query string, it would look like this:
RewriteCond %{QUERY_STRING} !(^|&)redirect=no($|&)
RewriteRule . /index.php [L]
So if the RewriteCond fails (it has a redirect=no in the query string), then do not apply the following rule, which rewrites whatever the request URI is to /index.php. The actual rule that you need may be different, but using RewriteCond and %{QUERY_STRING} is the basic idea that you want.

Related

Apache Mod_Rewrite empty query string followed by slash

I have some super weird URLs I need to redirect. The original URLs look like this:
server1.com/directory?/tfoo
These URLs needs to go here:
server2.com/search~?query=foo
I've tried a bunch of different possibilities, but this is what I'm working with right now:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/t(.*) https://server2.com/search?query=$2 [L,NE,NC]
Basically, I thought I would have to look for a blank query string, and then try to move on to grab the value given in the directory structure (foo). The RewriteCond matches my original URL, but I can't seem to grab the actual query parameter.
I've tried a bunch of things with RegEx to try to ignore the questionmark altogether, but it looks like mod_rewrite only understands the questionmark in the context of a query parameter redirect.
Any advice is hugely appreciated.
Thanks!
You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /directory/?/t(.+)\s [NC]
RewriteRule ^ http://server2.com/search~?query=%1 [NE,L,R]
Deadooshka's solution (commented above) worked:
RewriteCond %{QUERY_STRING} "^/t(.*)$"
RewriteRule "^/?directory$" "https://server2.com/search?query=%1" [L,NE,NC]`

.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.

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?

rewrite url using htaccess

How can I change url in this way using htaccess:
http://example.com/page.php?go=something.php
should be redirected to:
http://example.com/something
If the get parameter name is different than 'go' leave as it is...
This should do:
# Assuming that "RewriteEngine On" has already been called
RewriteCond %{QUERY_STRING} ^(.*&)?go=([a-z]+)\.php(&.*)?$
RewriteRule page.php %2?
What happens here? First, RewriteCond matches a query string that contains the go=something.php, where "something" is captured by ([a-z]+). Then the RewriteRule uses the second capture group's contents from RewriteCond, containing "something.php". The question mark at the end gets rid of the original query string.
Note: if you want to preserve the rest of the query string excluding go=... parameter, things get a bit more complicated.
See the docs in http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html for more info.
something like this (google around for the correct syntax)
RewriteEngine On
RewriteBase /
RewriteRule ^page.php?go=login.php /login [L]