-1

i want a single php script to handle all incoming requests to my /blog directoy. The php script checks wether a SQL record exists for the url (i.e. www.example.com/blog/example_article). If a record is found it serves the corresponding page data. If there is no record for a url (i.e. www.example.com/blog/nothing_here) it redirects to 404.

I need a rewrite rule in nginx for that.

My current config:

    root /usr/share/nginx/html/example/;
    index index.php;

    server_name example.com www.example.com;

    location / {
            try_files $uri $uri/ @extensionless-php;
    }

    location /blog {
            rewrite ?; // here i need a rewrite rule
    }

    location /uploads {
            deny all;
    }

    error_page 404 /templates/404.php;

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            fastcgi_intercept_errors on;
    }

    location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
    }

Please note that i am using extensionless URL's.

J4rVan
  • 11
  • 3
  • i have managed to get this far : location /blog { rewrite ^/([a-z0-9-]+) /article.php?slug=$1; } but i get my 404 page – J4rVan Apr 18 '16 at 00:21

1 Answers1

1

Answer:

location /blog {
    index article.php;
    rewrite ^/blog/(.*)$ /blog/article.php?slug=$1;
}
J4rVan
  • 11
  • 3
  • While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono Apr 18 '16 at 09:28