0

I have a regex right now on a WordPress site using Redirection plugin:

Source URL: ^/video/(.+)

Target URL: /video/$1

And that's working fine. Now, I need to add ?filter=views to the end of the Target URL, but I can't seem to find a way to do it.

Desired redirection would be:

site.com/video/video-category-name to site.com/video/video-category-name?filter=views

Adding /video/$1?filter=views as Target URL adds multiple ?filter=views when I just need the variable added once.

Any one that can help me out? Thanks!

  • 3
    Target URL = `/video/$1?filter=views` does not work ? – Stephane Janicaud May 13 '17 at 21:22
  • @stej4n my bad, with the description I added it does work. However what I am trying to do apart from that, is to redirect `/video/whatever-keywords` to `/video/whatever-keywords?filter=views`, and that doesn't work (it adds multiple `?filter=views` after the URL and I just need one). I updated the original question with this. – Álvaro Castaño May 13 '17 at 21:30

1 Answers1

0

You should exclude multiple redirections by using

Source URL: ^/video/([^?]+)

Target URL: /video/$1?filter=views

Explanation

if Source URL is ^/video/(.+) then (.+) will match anything after /video/ including after the first redirection :

/test match .+ => REDIRECT TO /video/test?filter=views

/test?filter=views match .+ => REDIRECT TO /video/test?filter=views?filter=views

... etc.

If you allow any character except ? after /video/ then redirection will stop immediately because /video/$1?filter=views won't match Source URL anymore

Stephane Janicaud
  • 3,531
  • 1
  • 12
  • 18
  • For some reason the plugin is deleting everything after `?` in Source URL after I hit save. – Álvaro Castaño May 13 '17 at 21:44
  • By the way, other filter URLs should work (?filter=rating, ?filter=new). I need that when you visit the URL without a filter it adds `?filter=views` by default. – Álvaro Castaño May 13 '17 at 21:45
  • I added `([^?]+)` (without # as per your recent edit, and it's still adding multiple variables to the URL) – Álvaro Castaño May 13 '17 at 21:49
  • if the plugin is deleting everything after `?` then it's logical and it redirects again and again. Which plugin are you using ? Maybe it has an option to keep URL args ? – Stephane Janicaud May 13 '17 at 22:32
  • It seems Wordpress Redirection plugin does not handle query parameters. You should use .htaccess instead and [QSA] flag. http://stackoverflow.com/questions/12551382/what-does-1-qsa-l-mean-in-my-htaccess-file – Stephane Janicaud May 13 '17 at 22:43
  • Im using WordPress Redirection yes. I tried using .htaccess with this, and it's doing nothing: `RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^/video/([^?]+) /video/$1?filter=views [QSA,L]` – Álvaro Castaño May 13 '17 at 23:09
  • As query parameters is optional in Redirection plugin or .htaccess, I don't think it's possible to redirect a base URL (with query parameters or not) to the same one without loop. But maybe I'm wrong. Could'nt `views` be your default filter instead to prevent this kind of redirection ? – Stephane Janicaud May 13 '17 at 23:19