htaccess rewrite to remove query string - apache

I need to rewrite the following:
http://www.mystuff.com/drinks/category/beer?page=1
to
https://www.mystuff.com/food-drink/beer/ale
No matter what I try the URI rewrite to the new address but it keeps the query string attached. I need to lose this. I've tried so many options and none seem to work, can anybody ofgfer some advice.
I thought this would do it, but no:
RewriteCond %{QUERY_STRING} (.*)(?:^|&)page=(?:[^&]*)((?:&|$).*)
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(/drinks/category/beer)$ https://www.mystuff.com/food-drink/beer/ale [R=301, L]
Can anybody help?

You need to add an empty query string to truncate it on the rewrite. Add a single ? to the end of the rewrite:
RewriteCond %{QUERY_STRING} (.*)(?:^|&)page=(?:[^&]*)((?:&|$).*)
RewriteCond %1%2 (^|&)([^&].*|$)
RewriteRule ^(/drinks/category/beer)$ https://www.mystuff.com/food-drink/beer/ale? [R=301, L]

Related

Modify query string value using htacces

I want to redirect
https://www.example.com/signup?plan=basic to https://www.example.com/signup?plan=basic-monthly and https://www.example.com/signup?plan=pro to https://www.example.com/signup?plan=pro-monthly .
How can I achieve this using htaccess ?
There are many questions related to this here. But, couldn't find an answer for this specific scenario.
This is the code I tried and failed:
RewriteCond %{QUERY_STRING} (^|&)plan=pro(&|$)
RewriteRule ^signup /$0?plan=pro-monthly [R=301,L]
Also, while trying the same with "basic" instead of "pro", the word "basic" i shown in red color as if it is a keyword.
Could you please try following, written with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(plan=(?:basic|pro))$ [NC]
RewriteRule ^(signup)/?$ $1?%1-monthly [L]
2nd solution: Or you could try following too. Make sure you either put 1st solution rules OR this one at a time.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(signup)\?(plan=(?:basic|pro))\s [NC]
RewriteRule ^ %1?%2-monthly [L]

Apache .htaccess rewrite parameter to directory

I've got an application that has been migrated to a newer platform. The tasks are similar and I'd like to redirect a GET parameter to a directory. For example
http://gallery/index.php?gal=ABC => http://photos/gal/ABC
and
http://gallery/?gal=DEF => http://photos/gal/DEF
and the anything that doesn't get caught redirect it to http://photos
I've tried
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^(/index.php)$ /%1/%2?
However all I get is a 404 and no redirection. Similarly, I've tried
RedirectMatch ^/index\.php\?=gal(.*) http://photos/gal/$1
but I'm having trouble escaping the ? in the original URL.
What would be the proper way of going about this?
Create a .htaccess file and insert the following code:
RewriteCond %{QUERY_STRING} ^(.+)=(.+)$
RewriteRule ^index.php$ http://photos %1/%2? [L,NC,R=301]
Your order is reversed. Rewrite it in front of you
RewriteRule /(.+)\/(.+) index.php?$1=$2
The question is old but might be still relevant for others, so I suggest a slightly different general approach:
RewriteEngine On
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z]+) [NC]
RewriteRule (.*) /%1? [R=302,L]
Notes:
QUERY_STRING in the condition checks for a param name "key" and catches it's value
The rewrite rule adds the param value as directory using %1. Note the ? removes the original query part from end result

Apache redirect query string

I'm new in building reverse proxy apache.
I've a query string like this:
host.example.com/some/thing/?company=string with spaces l.t.d.
And i want it to be seen like:
host.example.com/some/thing/string with spaces l.t.d.
I've written this:
RewriteCond %{QUERY_STRING]} ^company=(.*)$
RewriteRule ^/some/thing$ /some/thing/%1 [NC,L,R=301]
But both the RewriteCond and RewriteRule don't work.
Any help it's really appreciate.
Thanks
You are actually not matching against the %{QUERY_STRING} variable, remove the ] from the variable .
Try :
RewriteCond %{QUERY_STRING} ^company=(.*)$
RewriteRule ^/?some/thing$ /some/thing/%1? [NC,L,R=301]

Match Question Mark in rewrite rule using .htaccess

I have tried it in a different way that is also not working for me.
Match Question Mark in rewrite rule
please help me on this
Redirect to different url
http://localhost/crb/index.html?q=xxxxxxxx
To
http://localhost/crb/demo/result?q=xxxxxxxx
I tried this
Options +FollowSymlinks
RewriteEngine on
rewriterule ^crb/index.html?q=xxxxxxxx(.*)$ http://localhost/crb/demo/result?q=xxxxxxxx$1 [r=301,nc]
To rewrite based on query string use :
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^q=xxxxxxxx(.*)$
RewriteRule ^/?crb/index.html http://localhost/crb/demo/result?q=xxxxxxxx%1 [R=301,NC]
You could try the Query String Append (QSA) flag:
RewriteRule ^crb/index.html$ crb/demo/result [NC,L,QSA]
This should append the existing query string (q=xxxxxxxx) to the new url.
https://wiki.apache.org/httpd/RewriteFlags/QSA
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
If this code doesn't work, modify it a little and try again.

Htaccess redirect depending on the value of variable

I have some difficulties with such an easy problem.
On our site we've got a paginator, wich works with GET-variable "p" (p=1 - is the first page, p=2 - second and so on).
I do not want the script to consider the value 1 of this variable (p=1). So I've got the query string like:
http://www.mysite.ru/mypage/some_page2?p=1
and want to redirect user via htaccess to page:
http://www.mysite.ru/mypage/some_page2
It would be nice, if htaccess rule handle not only value "1", but also "","bla-bla-bla" like
http://www.mysite.ru/mypage/some_page2?p=$#^&*_not_right_value
http://www.mysite.ru/mypage/some_page2?p=
Thank you for your attention.
UPD:
The working solution for my case:
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.*)$ /$1? [R,L]
Query string manipulation via .htaccess can generally only be done via mod_rewrite. I haven't tested it, but the following should be close to what you need.
RewriteEngine on
RewriteCond %{QUERY_STRING} p=1 [OR]
RewriteCond %{QUERY_STRING} p=[^0-9]
RewriteRule (.*) http://www.mysite.ru/$1 [R, L]
The second condition simply tests that the p= is followed by a number. Once tested, the rule can be expanded to test that only a number exists after p (ie, right now p=2garbage would work) but it is better to start off with just the basics.
The working solution for my case:
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.*)$ /$1? [R,L]