rewrite url using htaccess - apache

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]

Related

htaccess redirect with variable query params

could you help me get this working please.
I am trying to redirect
/search.php?id=5GHU&distance=50&sort=title
or
/search.php?id=5GHU
or
/search.php?id=5GHU&distance=50
to
/search/?query_string_values
i.e. whether it's one query string parameter or many they all should go there
This is what I tried.
RewriteBase /
RewriteCond %{QUERY_STRING} (?:^|&)id=([^&]+)
RewriteRule ^search\.php$ /search/%1? [L,R=permanent]
Thanks
Sorry for my confusion about your question, I got it now. So...
Modify your .htaccess like this:
RewriteEngine On
RewriteRule "^/search.php$" "/search/" [L,R=301,QSA]
In short it will take the query string (what follows /search.php that starts with ?) and append it to "/search/". That is done by the QSA flag to RewriteRule. No matter how many parameters you have, it will append it all.
See https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
So
/search.php?a=1&b=2 --> /search/?a=1&b=2
/search.php?c=3 --> /search/?c=3
Since /search/?a=1&b=2 does not specify a page to use, it will use the default page defined by DirectoryIndex in your configuration. I prefer to explicitly specify the page, but it works without.

How I can rewrite a query-string based URL only partially

I want to rewrite the following URL, based on part of its query string, but at the same time, I want to keep the rest of the Query String.
Original URL:
http://example.com/index.php?route=product/show&item_id=25&show_mobile=true
To be converted to:
http://example.com/product/show/?item_id=25&show_mobile=true
I have searched and found the following wiki on Apache:
https://wiki.apache.org/httpd/RewriteQueryString
But it hasn't a section for rewriting a single Key&Value pair in Query string and keeping the rest.
This should get you close, it's based on the "remove a key" entry from the wiki. You want to isolate the part that will go into the path in the first capture and use the 2nd capture for the remaining query string.
RewriteCond %{QUERY_STRING} ^(route=[^&]*)&(.*)
RewriteRule ^index.php /%1?%2
You can use this generic rule to capture parameter value of route in any order from query string and reuse in target URL:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?(.*&)?route=([^&]*)&?(\S*)\sHTTP [NC]
RewriteRule ^index\.php$ /%2?%1%3 [R=301,NE,L]

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

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.

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?