Welcome! Log In Create A New Profile

Advanced

Redirect all url to single php

Posted by J4rVan 
Redirect all url to single php
April 17, 2016 07:42PM
Hi, 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.
Re: Redirect all url to single php
April 17, 2016 08:26PM
i have managed to firgure out a rule but i still get a 404 error

location /blog {
rewrite ^/([a-z0-9-]+) /article.php?slug=$1;
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 174
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready