13

I'm setting up nginx and I need to use the location directive to match all requests that begin with /site1 AND end with .php. What regex do I use to do this?

I know I can use ^ to match the beginning of the string and $ to match the end of string. So I'm able to match the beginning of the string OR the end of the string as ^(/site)|(\.php)$. Is there any 'and' operator so I can write something like ^(/site)&(\.php)$ ?

1 Answer 1

19

You want this: ^(/site).*(\.php)$. This means that first you match the beginning followed by "/site", then anything any number of times, and finally ".php" followed by the end of the string. If you don't need the captures, it would be simpler as: ^/site.*\.php$.

Sign up to request clarification or add additional context in comments.

1 Comment

for security reasons I'd put another / after /site so that /sitexxx.php is not matched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.