htaccess remove query string and then redirect - .htaccess

I need to redirect a url based on query string and need to remove the query string on redirect.
Source url: http://www.example.com/?id=15&L=1&link=androidapp
Target url: http://www.example.com/test.php&id=15&L=1
'?id=' is a dynamic parameter. It is changed everytime.
I wrote following condition. It redirects, but I didn't get the desired target url.
RewriteCond %{QUERY_STRING} link=androidapp
RewriteRule ^(.*)$ http://www.example.com/test.php? [R=301,L]
Please help me.

You can use this .htaccess:
RewriteCond %{QUERY_STRING} id=(\d+)&L=(\d+)&link=androidapp [NC]
RewriteRule ^ test.php?id=%1&L=%2 [R=301,L]

Related

Add query string to redirect URL in .htaccess

How can I add s string to the end of a search result using .htaccess?
Original link: www.example.com/?s=search1 redirect to:
www.example.com/?s=search1&post_type=product
or
www.example.com/?s=search2 redirect to:
www.example.com/?s=search2&post_type=product
"search1" and "search2" are variable.
I need this to change a Wordpress search result to a Woocommerce search result.
What a tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^/?s=$ http://www.example.com/?s=%1&post_type=product [L,R=301]
Thank you in advance.
Your .htaccess seems fine, except for one thing: the RewriteRule Pattern will be matched against the part of the URL after the hostname and port, and before the query string, so this should do:
RewriteEngine On
RewriteCond %{QUERY_STRING} !post_type=product
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^$ http://www.example.com/?s=%1&post_type=product [L,R=301]

301 Redirect Get Parameter URL with htaccess

I need to 301 redirect an old url that contained a get parameter in the url.
I need to 301 the URL:
http://www.website.com/choose?cat=womens
to this URL:
http://www.website.com/womens
I have searched and tried without it working:
RewriteCond %{QUERY_STRING} cat=womens
RewriteRule ^choose\.php$ /womens [L,R=301]
Where am I going wrong?
You're almost correct, just 2 issues:
.php wasn't there in your original URI after choose as per the question
You need to add ? in target to strip original query string
You can use:
RewriteCond %{QUERY_STRING} (?:^|&)cat=([^&]+) [NC]
RewriteRule ^choose(?:\.php)?$ /%1? [L,R=301,NC]
Try these:
RewriteCond %{QUERY_STRING} ^cat=womens$
RewriteRule ^choose$ http://www.website.com/womens? [R=301,L]

.htaccess URL redirection appending query string with target URL

I am new to .htaccess redirect.
I have a source URL with too many parameters query string.
Source
http://localhost/se/karriar/?utm_source=A_candy_box&utm_medium=Printed_media&
utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&
utm_campaign=Mint_pastille_campaign
Target: http://www.sample-website.com
When I tried to call source URL its getting redirected along with query string as like below.
http://www.sample-website.com?utm_source=A_candy_box&utm_medium=Printed_media&
utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&
utm_campaign=Mint_pastille_campaign
I have written the below script in .htaccess file.
RewriteCond %{HTTP_HOST} ^(www.)?localhost$ [NC]
RewriteCond %{QUERY_STRING} ^utm_source=A_candy_box&utm_medium=Printed_media&utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&utm_campaign=Mint_pastille_campaign$ [NC]
RewriteRule ^se/karriar(.*)$ http://www.sample-website.com [R=301,L,NC] `
I would like to not appending query strings in the targeted URL. Can you any one help me?
Replace your .htaccess with this
RewriteCond %{HTTP_HOST} ^(www.)?localhost$ [NC]
RewriteCond %{QUERY_STRING} ^utm_source=A_candy_box&utm_medium=Printed_media&utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&utm_campaign=Mint_pastille_campaign$ [NC]
RewriteRule ^se/karriar(.*)$ http://www.sample-website.com? [R=301,L,NC]
Append ? after your target domain.

Remove variable from base URL with htaccess

I've been trying to rewrite a URL such as
www.somesite.com/?x=372
into a url
www.somesite.com/
My current code does not seem to work
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule http://www.somesite.com/ [R=301,L]
I've looked up countless ways of trying to do it with htaccess and still no success.
If you simply want to redirect a client to remove the query string (everything after the ? in the URL), then you can try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^ http://www.somesite.com/? [R=301,L]
You've got most of it right, it seems, but your rule needs to have a match, and your target (the http://www.somesite.com/) needs a ? at the end so that any query string before the rewrite won't get appended.
In Apache 2.4 or newer you can use the QSD query string discard flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule .* http://www.somesite.com/ [R=301,L,QSD]

How to stop htaccess rewrite rule carrying over query string

I am setting up some redirects. I want to redirect the following URL:
/cms/index.php?cat_id=2
to the following URL:
/flash-chromatography
The rule I currently have is as follows:
RewriteCond %{QUERY_STRING} ^cat_id=2$ [NC]
RewriteRule ^cms/index\.php$ /flash-chromatography [L,R=301]
This rule is almost perfect apart from it redirect the URL to the following:
/flash-chromatography?cat_id=2
So you see my problem is it has kept the ?cat_id=2 part when I don't want it to.
How do I stop it keeping this bit?
Just add ? at the end of rewritten URL:
RewriteCond %{QUERY_STRING} ^cat_id=2$ [NC]
RewriteRule ^cms/index\.php$ /flash-chromatography? [L,R=301]

Resources