skip to Main Content

I have 3 url for example

  1. Homepage: www.example.com/
  2. Admin: www.example.com/wp-admin
  3. Products: www.example.com/products

3rd url could be any url after main domain like so: www.example.com/something

What i want is to redirect url if only the url is something after main domain but cannot be /wp-admin or main domain www.example.com.
Only redirect if www.example.com/something

Conclusion: Home page and /wp-admin intact, otherwise redirect. Hoping to solve it with regex if possible as any other access to cPanel or source code is not possible. Thank you 🙏

I’ve attempted countless regex but couldn’t do it.

2

Answers


  1. You could use the following regex :

    ^www.example.com(/?|(/wp-admin))$

    which match the domain followed by either nothing or /wp-admin.

    It will match

    All of the others URLs would not match the regex, so you can juste it as a condition and reverse it if needed.

    Login or Signup to reply.
  2. "… What i want is to redirect url if only the url is something after main domain but cannot be /wp-admin or main domain http://www.example.com. Only redirect if http://www.example.com/something …"

    You can use the following pattern.

    ^www.example.com/(?!wp-admin).+
    

    This will match these,

    www.example.com/products
    www.example.com/something
    www.example.com/abc/def
    

    And, not these,

    www.example.com
    www.example.com/
    www.example.com/wp-admin
    

    Essentially, the (?!wp-admin) is a look-ahead, which is asserting that the text at that point, will not equal "wp-admin".

    Here is the Wikipedia article, outlining regular expression assertions.
    Wikipedia – Regular expression – Assertions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search