0

I'm a little bit lost here. In my nginx conf file I want a rule that states this:

if $request_uri does not contain following words (css|images|js) then rewrite all to /index.php

this is what I got, but it doesn't work:

if ($request_uri ~ !(images|css|js)) {
    rewrite $ /index.php;
}

I think the regex isn't matching up?

Edit: this is the solution

if ($request_uri !~* (images|css|js)) {
    rewrite $ /index.php;
}

1 Answer 1

1
  1. IfIsEvil and expensive.
  2. Use Positive Logic instead.

Better to put all static content in a directory.

-static 
 |--images
 |--css
 |--js


server {
  server_name www.foo.com;
  root /your_nginx_root/;

  location / {
    index  index.php;
  }

  location /static {
    add_header Cache_control "public";
    expires max;
  }

  error_page 301 302 400 401 402 403 404 405      /index.php;
  error_page 406 408 409 410 411 412 413 414      /index.php;
  error_page 415 416 495 496 497 500 501 502      /index.php;
  error_page 503 504 507                          /index.php;
}


*EDIT:*Try this

if ($request_uri !~* (images|css|js)) {
    rewrite $ /index.php;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I know, my static files are in a static folder, but eventually the regex match will have to be something like this (js|css|sitemap|images|fonts|media|uploads|pdf|callbacks)
if they are in directories better to use this approach, if not, let me check your conditional.
@Bundy made an edit please check if works, I can't test it here.
Thanks, it almost worked :) Added the rewrite that worked (for me) in my question

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.