1

I searched on the site, but i didn´t find an exact answer.

I have multiple urls in a wordpress site with different parameters like this:

http://www.example.com/folder/?filter_color=353&orderby=price-desc
http://www.example.com/folder/?filter_material=345&orderby=date
http://www.example.com/folder/?filter_size=43&price-asc

I need to permanent redirect all of them to http://www.example.com/folder/

Is it possible with htaccess? Thanks!

EDIT 1: I tried with this code but it doesn´t work. I want to redirect those old urls because they lead to a nothing found page. So after i copy this code and save the htaccess, nothing changes. The nothing found page still appear and the old url is shown in the browser. So it does nothing.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder$
RewriteCond %{QUERY_STRING} ^filter_material=([0-9]*)$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]

EDIT 2: I finally achieved what i wanted with this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]
Voltaki
  • 115
  • 7
  • Yes, it's possible. Please do some research, try to write the .htaccess code yourself, if you run into problems, share your code, along with expected behavior, observed behavior and a specific question. – Patrick Hund Dec 21 '17 at 12:51
  • I am sorry, but i don´t know how to do it. I forgot to write im just a wordpress "designer" I know almost nothing of htaccess. I hope someone can help me writing the code. Thanks! – Voltaki Dec 21 '17 at 12:56
  • OK, take a look at this tutorial, I think it's pretty clear: https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/ – Patrick Hund Dec 21 '17 at 13:04
  • And this related question: https://stackoverflow.com/questions/10135702/301-redirect-from-url-with-query-string-to-new-domain-with-different-query-strin – Patrick Hund Dec 21 '17 at 13:05
  • First of all, thank you Patrick. I tried with this code, but it is not working. `RewriteEngine On RewriteCond %{REQUEST_URI} ^/folder$ RewriteCond %{QUERY_STRING} ^filter_metal=([0-9]*)$ RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]` – Voltaki Dec 21 '17 at 13:36
  • Now we're getting somewhere. Please edit your original question, add the code you have tried, describe what you want to happen, and what happens exactly (“it's not working” is too vague). – Patrick Hund Dec 21 '17 at 13:40
  • 1
    I have pasted the code in the original comment and explained what happens. – Voltaki Dec 21 '17 at 13:45
  • Thanks, I've removed my close vote and added an answer – Patrick Hund Dec 21 '17 at 14:02

2 Answers2

0

The rewrite condition for the query string is a regular expression, which is a tool for matching against search patterns.

The regular expression you are using – ^filter_material=([0-9]*)$ would match against URLs with a query string that starts with filter_material=, followed by an arbitrary number of digits.

The URL you want to redirect, however, does not match here, because it also contains &orderby=date, which is not all digits.

Since all the URLs you want to redirect start with filter_, you can simply use a regular expression that matches any string that starts with that, like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteCond %{QUERY_STRING} ^filter_.+$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]

The period means “any character”, the plus means “none, one, or more than one”.

Note that I've also added /? to the rewrite condition for the request URI, which means “there could or could not be a slash after folder”.

You can test this rewrite rule here: http://htaccess.mwl.be?share=2329d6f6-4fd5-5ff2-9035-a1957a242a42

Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
  • Patrick! Thank you for your time and dedicated explanation. I tried changing that line, but still nothing changed. Can you please double check if every character is well written? Maybe i missed something. Thanks! – Voltaki Dec 21 '17 at 14:38
  • Ah, there was also a slash at the end of the condition for the request URI missing. Check this out, this works: http://htaccess.mwl.be?share=7f08649c-c491-5b0c-9e96-a0fee8128516 – Patrick Hund Dec 21 '17 at 14:44
  • Thanks again Patrick. But in the example the original url is the same as the result url. :( Maybe i wasn´t clear, what I need is that all urls starting with filter_ after the /folder/ redirect to http://www.example.com/folder/ without any parameters or strings at the end. – Voltaki Dec 21 '17 at 14:56
0

I finally achieve what i wanted with this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]
Voltaki
  • 115
  • 7