Htaccess Rewrite single query string - string

I'm having an issue with a rather old siye. I have some generic URL's with a query string, that i want to 301 redirect, but I don't want to blanket re-direct the urls. I want to choose where each query string is being redirected as there are alot of different categories within the site. For example:
I want to change:
index.php?_a=viewCat&catId=199
to:
/garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html
But ill want to change another catid to another URL of my choosing, completely different structure. The problem I'm having is with the code i've got, if I don't have a ? at the end of the destination url, it works, but appends the query string to the end, if I put it on the end, it doesn't redirect at all.
Code I'm using:
RewriteCond %{QUERY_STRING} ^_a=viewCat&catId=199
RewriteRule ^index\.php$ /garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html? [L,R=301]
Any help would be appreciated!
EDIT: The rest of my htaccess
RewriteEngine On
RewriteRule ^conservatory/(.*)$ /conservatory-furniture/$1 [R=301,L]
RewriteRule ^dining-room/(.*)$ /dining-room-furniture/$1 [R=301,L]
RewriteRule ^garden/(.*)$ /garden-furniture/patio-furniture/$1 [R=301,L]
RewriteBase /
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule prod_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewProd&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule info_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewDoc&docId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule tell_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=tellafriend&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule _saleItems(\.[a-z]+)?(\?.*)?$ index.php?_a=viewCat&catId=saleItems&%1 [NC,L]

If these are complete URLs, you could anchor the pattern at the start of the string
RewriteRule ^cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
this would prevent an URL, which has cat_ inside being rewritten to index.php?....
And since you don't use the trailing optional part, you could eliminate this too
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1&%1 [NC]
Another point is the RewriteCond with query string. If the query string is optional, you could remove the RewriteCond and modify the RewriteRules to
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
So, all these would become
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
RewriteRule ^prod_([0-9]+) index.php?_a=viewProd&productId=$1 [QSA,NC]
RewriteRule ^info_([0-9]+) index.php?_a=viewDoc&docId=$1 [QSA,NC]
RewriteRule ^tell_([0-9]+) index.php?_a=tellafriend&productId=$1 [QSA,NC]
RewriteRule ^_saleItems index.php?_a=viewCat&catId=saleItems [QSA,NC,L]

Related

How redirect query search result from domain which contains a folder?

I have this code in my .htacces file:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ job\?search_keywords=%1 [NC,R,L]
And it works well for: example.com/?s=blabla
Now I would like to add that if the url contains /en/:
https://example.com/en/?s=blabla
change for https://example.com/en/work/?search_keywords=blabla
Somone could help me with it? Because my trying gives me ERROR 500.
You just need another rule to handle new URI pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^$ /job?search_keywords=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^en/?$ /en/work/?search_keywords=%1 [R=301,L]
Note that \\? in your RewriteCond is redundant as QUERY_STRING variable doesn't contain ?

use ? in first of url

I want use below link and load from category.php but it does not work
http://test.com/?search=x
.htaccess:
RewriteRule ^search$ category.php?search=$1 [QSA,NC]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test\.com$
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ http://category.php?search=%1 [R=302,L]
The reason you can't get away with a single RewriteRule is that query parameters cannot be directly rewritten. Instead, the above code captures the query string using RewriteCond %{QUERY_STRING}, which is then made available as %1 in the RewriteRule which follows. The RewriteCond %{REQUEST_URI} ensures that the code will only evaluate for the test.com domain.
Try this,
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ category.php?search=%1 [QSA,NC,L]
You can use this simple rule:
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^/?$ category.php [L]
Pattern ^/?$ ensures that we match only landing page, nothing else.
QUERY_STRING is automatically appended to target URI so there is no need to capture and append in target URI.

Htaccess appending to query string with QSA without redirect

I am trying to add English version to my website in subdomain level as following:
en.mywebsite.com/business-name : This will show the English version of a sub-page.
Right now I have actually achieved it somehow by appending the subdomain to the query string with the following code:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com [NC]
RewriteCond %{HTTP_HOST} !^en\.mywebsite\.com [NC]
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteRule ^(.*)$ http://www.mywebsite.com/$1?language=en [QSA]
The problem is, it is doing it by redirecting the user to www.mywebsite.com/business-name?language=en, but I want to avoid redirection, and pass the "newly generated" query string virtually instead.
Can it be achieved, or does QSA always redirect?
Thanks in advance.
Try changing your last rule from:
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteRule ^(.*)$ http://www.mywebsite.com/$1?language=en [QSA]
to
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ /$1?language=en [L,QSA]

htaccess remove query string only for root

I want to remove a query string but only for the 6 cases below and never for any subfolders / other url's.
http://www.example.com/?___store=de&___from_store=de
http://www.example.com/?___store=de&___from_store=en
http://www.example.com/en/?___store=de&___from_store=en
http://www.example.com/en/?___store=de&___from_store=en
http://www.example.com/en/?___from_store=en
https//www.example.com/en/?___from_store=de
This is what I already got. It's working, but removes the query string from all url's, what I don't want.
RewriteCond %{QUERY_STRING} /?___store=de&___from_store=de
RewriteRule ^(.*)$ /$1? [L,R=301]
RewriteCond %{QUERY_STRING} /?___from_store=de
RewriteRule ^(.*)$ /$1? [L,R=301]
RewriteCond %{QUERY_STRING} /?___store=de&___from_store=en
RewriteRule ^(.*)$ /$1? [L,R=301]
RewriteCond %{QUERY_STRING} /?___from_store=en
RewriteRule ^(.*)$ /$1? [L,R=301]
I need this specific rewrite rule for a Magento shop. The store view code of the default store is removed by this extension: Knectar/Magento-Store-Codes, see also this: Magento remove store code for default store view.
As a result, the ___store query parameter is added to the url, but only on root.
My solution above does not work, because when it comes to products / categories the language switch is not working correctly anymore (and there is no need to remove the string, because it is stripped out by Magento itself).
Try the following:
RewriteCond %{QUERY_STRING} ^___store=de&___from_store=(en|de)$ [NC]
RewriteRule ^(en/?)?$ $1? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^___from_store=(en|de)$ [NC]
RewriteRule ^en/?$ $0? [R=301,L,NC]

redirect url with complex query string

I've tried using RewriteCond and RewriteRule to redirect a complex URL to a new page but without success.
The original URL:
index.php?option=com_k2&view=item&id=127&Itemid=353
or http://www.example.com/index.php?option=com_k2&view=item&id=127&Itemid=353
Have tried:
RewriteRule ^index.php?option=com_k2&view=item&id=127& Itemid=353$ http://www.example.com/somepage [R=301,L]
Also:
RewriteCond %{QUERY_STRING} index.php?option=com_k2&view=item&id=127&Itemid=353
RewriteRule http://www.example.com/somepage [R=301]
And:
RewriteCond %{QUERY_STRING} ^id=127&Itemid=353$
RewriteRule ^$ http://www.example.com/somepage [R=301]
What am I missing?
Try:
RewriteCond %{QUERY_STRING} (^|&)id=127&Itemid=353(&|$)
RewriteRule ^(?:index\.php|)$ http://www.example.com/somepage [L,R=301]

Resources