.htaccess query string being ignored - .htaccess

I've got these 2 query strings in my .htaccess file that check if the query string contains name_const or CHART but the rewrite rule redirect only gets triggered when query strings contains name_const and not when it contains CHART.
What am I doing wrong here? Can I not do this same line twice or something?
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)name_const($|&) [NC]
RewriteCond %{QUERY_STRING} (^|&)CHAR($|&) [NC]
and the rewrite rule:
RewriteRule .* page [QSD,R=301,L]

RewriteEngine On
RewriteCond %{QUERY_STRING} ^.*name_const.*$ [NC,OR]
RewriteCond %{QUERY_STRING} ^.*CHART.*$ [NC]
RewriteRule .* /page [QSD,R=301,L]
Use the [OR] flag to match either name_const or chart in the query string.

Related

How to exclude query string with specific parameters from .htaccess redirect

I have a Wordpress site and I am discarding query strings with the following rules:
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]
The above rules work fine, but I want to exclude some URLs with specific parameters in the query string. I have tried to insert the following rules between the two rules above, but I am apparently missing something and the exclusion doesn't work:
RewriteCond %{QUERY_STRING} !^/?post_type=project
RewriteCond %{THE_REQUEST} !^/?post_type=project&p=14507&et_fb=1&PageSpeed=off
RewriteCond %{REQUEST_URI} !^/?post_type=project
RewriteCond %{REQUEST_URI} !^/?post_type=project$
My goal is to exclude URLs if they contain some or all of the parameters:
post_type=project
et_fb=1
PageSpeed=off
You may use this rule right below RewriteEngine On line (and above front controller WP rule):
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)post_type=project(?:&|$) [NC,OR]
RewriteCond %{QUERY_STRING} !(?:^|&)et_fb=1(?:&|$) [NC,OR]
RewriteCond %{QUERY_STRING} !(?:^|&)PageSpeed=off(?:&|$) [NC]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L,NE]
# more rule below this line
I am using 3 separate RewriteCond to be able to match these query parameters anywhere in query string (note use of OR clause).

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.

How can I redirect URL where parameters have not value via .htaccess to 404 page?

I have got an order to make redirection on site to 404 page, if reaquest parameter not specified value, i.e. "/?qwerty" (where "qwerty" means invalid parameter, without '=' and any value) must be redirected. I have wrote .htaccess file (site works with Joomla). You can see the fragment below:
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=http:// [OR]
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=(\.\.//?)+ [OR]
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=/([a-z0-9_.]//?)+ [OR]
#Next string redirects all of requests with parameters
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_] [NC]
RewriteRule ^(.*)$ index.php [R=404,L]
But now site can't process any request with parameter. How can I solve this problem?
To redirect to 404 if the query value parameter is missing, you can use :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^querty$ [OR]
RewriteCond %{QUERY_STRING} ^querty=$
RewriteRule ^$ - [R=404,L]
This will redirect example.com/?querty or example.com/?querty= to /404 .

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]

Resources