.htaccess - Remove certain parameters at random positions - .htaccess

I need your help please. I need to optimize this as best as possible. Thank you!
X is a random value, but parameter is always "distance" or "type". The "orderBy" parameter with exactly these values.
I need to remove these params:
orderBy=popular
orderBy=distance
distance=X
type=X
https://example.com/posts/anything?orderBy=distance&distance=400&q=XXX&page=2&type=3&amore=X&another=44
https://example.com/posts/anything?distance=400&q=XXX&orderBy=distance&page=2&type=3&amore=X&another=44
https://example.com/posts/anything?q=XXX&distance=400&page=2&type=3&amore=X&another=44&orderBy=distance
# With PARAMETER at the START
RewriteCond %{QUERY_STRING} ^(.*)&?^distance=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1%2 [NE,R=301,L]
# With PARAMETER at the END
RewriteCond %{QUERY_STRING} ^(.*)distance=[^\&]*$
RewriteRule ^(.*)$ /$1?%1 [NE,R=301,L]
# With PARAMETER in the MIDDLE
RewriteCond %{QUERY_STRING} ^(.*)\&distance=[^\&]*\&(.*)$
RewriteRule ^/?(.*)$ /$1?%1&%2 [NE,R=301,L]
# Remove the "orderBy" distance AT THE START
RewriteCond %{QUERY_STRING} ^(.*)&?^orderBy=distance+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1%2 [NE,R=301,L]
# Remove the "orderBy" distance AT THE END
RewriteCond %{QUERY_STRING} ^(.*)orderBy=distance+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1%2 [NE,R=301,L]
# Remove the "orderBy" distance in the MIDDLE
RewriteCond %{QUERY_STRING} ^(.*)\&orderBy=distance[^\&]*\&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1&%2 [NE,R=301,L]
So on..

Related

Redirect - 4 total parameters

#anubhava and #RavinderSingh13 have continued to help me learn and progress to where I am today with this script. This is what I have so far:
##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]```
Now, I would like to add one more rule with only one parameter instead of two. However, this parameter uses PHP $_GET['name'] in the url. I am trying to add one more rule that rewrites http://IPADDRESS.com/file.php?name=JohnDoe to http://IPADDRESS.com/newdirectory/JOHNDOE.
Thank you!
With your shown samples please try following. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
##Rule with two parameters: $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?name=(\S+)\s [NC]
RewriteRule ^ /directory/%1? [R=301,L]
RewriteRule ^directory/([^/]*)/?$ /directory/file.php?name=$1 [NC,L,QSA]

Are 3 identical parameters possible with .htaccess?

#anubhava and #RavinderSingh13 have helped me tremendously so far in understanding more about .htaccess and rewrite rules. However, even though both the first rule (with one parameter: $id) and the second rule (with two parameters: $id and $name) works for rewriting the url, my third rule I attempted with also two parameters ($id and $class) fails, and does not rewrite the final url. Below is my entire file so far. I would like to add that the $class parameter is in the format of a-class, b-class, etc., so maybe that could be a contributor to the the rewrite not working? Thank you for helping me continue to learn thus far!
RewriteEngine on
--1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
--2nd rule with two parameters: $id and $name--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
--My attempt at the third rule following the example from the second, but this rule fails. Parameters are $id and $class--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
With your shown samples and considering class word will be there always in your 3rd url try following rules. Also you need not to write RewriteEngine ON many times, only 1 time starting of file is enough.
RewriteEngine on
##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]

htaccess add parameter HOST

how can I add a parameter to every call of a given domain?
example:
www.domain1.com it always has to be added at the end ?test1=abc
www.domain2.com it always has to be added at the end ?test1=def
i added code:
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteRule ^ %{REQUEST_URI}?test1=abc [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [NC]
RewriteRule ^ %{REQUEST_URI}?test1=def [L,R=301,QSA]
the parameter is added but there is a loop:
domain1.com/?test1=abc&?test1=abc&?test1=abc&?test1=abc&?test1=abc&
domain2.com/?test1=def&?test1=def&?test1=def&?test1=def&?test1=def&
very thanks for help
You should add RewriteCond to test if the URI containing test1 query string:
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} !test1=
RewriteRule ^ %{REQUEST_URI}?test1=abc [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [NC]
RewriteCond %{QUERY_STRING} !test1=
RewriteRule ^ %{REQUEST_URI}?test1=def [L,R=301,QSA]

htaccess: Remove parameter from URL

# case: leading and trailing parameters
RewriteCond %{QUERY_STRING} ^(.+)?session=[0-9a-z]+&(.+)$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
# case: leading-only, trailing-only or no additional parameters
RewriteCond %{QUERY_STRING} ^(.+)?session=[0-9a-z]+$|^osCsid=[0-9a-z]+&?(.*)$ [NC]
RewriteRule (.*) /$1?%1 [R=301,L]
URL is:
https://www.test.com/test/?session=123
Shouldn't this cut off the ?session=123?
If not, how can I achieve this?
You can use this rule to remove a parameter from anywhere in the query string:
RewriteCond %{QUERY_STRING} ^(.*&)?session=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [L,R=301,NE]

Redirect loop in htaccess

why is this causing a redirect loop? How do I have to change the code, to make it work?
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} de [NC]
RewriteRule ^$ http://website.com/?___store=german
RewriteCond %{HTTP:Accept-Language} nl [NC]
RewriteRule ^$ http://website.com/?___store=dutch
Thank you,
Toby
RewriteEngine On
RewriteCond %{QUERY_STRING} !\b___store=\w+\b
RewriteCond %{HTTP:Accept-Language} de [NC]
RewriteRule ^$ /?___store=german [L,QSA]
RewriteCond %{QUERY_STRING} !\b___store=\w+\b
RewriteCond %{HTTP:Accept-Language} nl [NC]
RewriteRule ^$ /?___store=dutch [L,QSA]
You don't need the http://website.com. .htaccess files loop so adding [L] isn't good enough; you need to detect the loop and looking for the store parameter is a good way. You also need the [QSA] flag if some requests use additional params.
Try this:
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} de [NC]
RewriteCond %{QUERY_STRING} !^___store [NC]
RewriteRule ^$ http://website.com/?___store=german [L]
RewriteCond %{HTTP:Accept-Language} nl [NC]
RewriteCond %{QUERY_STRING} !^___store [NC]
RewriteRule ^$ http://website.com/?___store=dutch [L]
If you go to website.com with AL of 'de', and then you get redirected to __store=german, your AL will still be 'de', so it will keep trying to redirect to that __store=german. Adding the [L] flag will stop apache from attempting to redirect multiple times.
This is another option, although the ___store parameter would have to be the same as the accept language. I think this should work (not exactly sure on the specifics of passing variables from a condition)
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} (de|nl) [NC]
RewriteCond %{QUERY_STRING} !^___store [NC]
RewriteRule ^$ http://website.com/?___store=%1 [L]

Resources