-1

I have been trying to understand and use answers from several different question in here for what i think is the same question, but with no luck...

I need a regular expresion to match the a url that contains &orders-panel as part of the url, an example url would be

https://example.com/wp-admin/post.php?post=xxxx&action=edit&orders-panel

can anyone help me?

Im thinking about using the Redirection plugin, as i need it to be triggered when not logged in, so it redirects to a different page.

Thanks Kind regards

  • Could you provide what you've tried so far and what the expected outcome is? – Lucan May 26 '20 at 13:02
  • Sure, I have tried many options (which unfortunetely i did not save them as they did not work, but the last one i tried is this which does not work either): /\b(?:https?:\/\/)?[^\/:]+\/.*?orders-panel The problem here is that because I do not EXACTLY know what am i doing, I dont know how to amend that to suit what i need. And basically i would need a regex that would get any url that contains &orders-page and redirect to example.com/login (idealy just when im not loged in already, if im logged in already, do not redirect anywhere, which i think can be achieved with redirection plugin) – Andres Molina Perez-Tome May 26 '20 at 13:13
  • I tried with this other one too .*\?((.*=.*)(&?))orders-panel as it says it works in https://www.regexpal.com/ but it seems it is not working either. – Andres Molina Perez-Tome May 26 '20 at 14:19

2 Answers2

1

You could use the below, this will match any http/https prefixed URL with &orders-panel in it

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)&orders-panel

The main URL pattern was taken from this answer. As you only want to redirect this should do the trick.

However, if you only want it to work with a particular domain, you could use something like below

https?:\/\/(www\.)?example.com\/.+?(&orders-panel)

It sounds like you're just dropping this into a redirect file but if not and you only want to redirect when the URL contains &orders-panel, you don't need regex to do this, a simple contains (in whatever language) would suffice.

Lucan
  • 2,907
  • 2
  • 16
  • 30
0

Thanks for the suggestion. While i was waiting for an answer i came up with a different solution, which is by using a function on my functions php file as:

add_action( 'init', 'orders-redirect');
   function orders-redirect() {
     if (!is_user_logged_in() && strpos($_SERVER['REQUEST_URI'], "orders-panel")){
        wp_redirect( site_url('/login/') ); 
        exit; 
    }   
}

And seems to be working just fine, my question would be.. do you see any major reasons to do not use my solution and use the regex one?