htaccess remove query string only for root - .htaccess

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]

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 Rewrite single query 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]

.htaccess remove query string, keeping SEO style url

Question:
Is it possible to clear the %{QUERY_STRING} without breaking any functionality of what I've accomplished in my .htaccess file.
I want to prevent users from adding ?foo=bar to the end of the url overwriting any predefined $_GET vars
(external re_write) Example:
From: example.com/foo/bar/hello/world?test=blah
To: example.com/foo/bar/hello/world
So far I have been able to accomplish this (internally):
From: example.com/foo/bar/hello/world
To: example.com/index.php?foo=bar&hello=world
.htaccess
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.(?:html?|php|aspx?) [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC]
# To externally redirect /dir/index to /dir/
RewriteCond %{THE_REQUEST} ^GET\s((.+)?(\/)?)?(index) [NC]
RewriteRule ^(([^/]+/)*)index$ /$1 [R=301,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
# Prevent Rewrite on existing directories, files, and links
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L] #^ - [L]
# To internally rewrite directories as query string
RewriteRule ^([^/=]+)=([^/]*)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)/([^/=]+)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)(/(.+))?\/?$ /$3?$1 [N,QSA]
What I've tried:
#if there is a query string
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1? [R=301,L] #remove query string
-
#if there is a query string
RewriteCond %{QUERY_STRING} !=""
RewriteRule (.*) $1? [R=301,L] #remove query string
-
#if there is a query string
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ $1? [R=301,L] #remove query string
I read somewhere, and experienced it, that applying the above breaks anything I've already accomplished. Completely clearing the whole url if they are masked $_GET vars. Even crashes apache 0.o (forgive me for any silly regex or .htaccess code, they're aren't my strong suite and most of it was snippets I found on SO)
is this possible? something I'm doing wrong?
thanks
Try:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L] #remove query string
You need to match against the actual request because you're building a query string with these rules:
# To internally rewrite directories as query string
RewriteRule ^([^/=]+)=([^/]*)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)/([^/=]+)(/(.+))?\/?$ /$4?$1=$2 [N,QSA]
RewriteRule ^([^/=]+)(/(.+))?\/?$ /$3?$1 [N,QSA]
When these rules loop around, the %{QUERY_STRING} variable will have stuff in it and your rules will clobber them. If you match against the actual request, your rewrites won't get affected.

Resources