开发者

nginx - ways to lessen number of files on server - 404 works but returns status 404

开发者 https://www.devze.com 2023-03-24 03:51 出处:网络
Our site contains hundreds of album files in a directory named /var/www/album/*.php. Each of these album files is named the number开发者_运维百科 of the album so: 245.php, 943.php, 103.php, etc. These

Our site contains hundreds of album files in a directory named /var/www/album/*.php. Each of these album files is named the number开发者_运维百科 of the album so: 245.php, 943.php, 103.php, etc. These album files consist of nothing more than a simple PHP directive to include the album.php script which also lives in /var/www/album/album.php (the meat behind displaying the album).

I would like to somehow eliminate the need to even have these album files in the directory so my thought was to configure nginx to serve up a custom 404 page if a requested page was not found. In the 404 page (custom_404.php) I have some php code which strips the _SERVER['document_uri'], checks the database to see if it is an album and if it is, the custom_404.php includes album.php. If the file requested is truly 404 then custom_404.php displays Page Not Found...

This works fine with the exception of the Status Code: 404 still being returned, however I cant help but think their must be a better way of handling this situation? I would like to stay away from going the album.php?album=245 route! Is this possible with nginx and php?

Thanks!


First tell nginx to serve index.php when the requested file is not found:

try_files $uri /index.php;

Then, in index.php, pull the required info out of the URI:

$uri = $_SERVER['REQUEST_URI'];
// logic to extract the album etc

So (for example) in your browser you hit http://yoursite.com/album/123 and in your index.php you get:

$uri = '/album/123';

(Then you can delete all the N.php files.)

0

精彩评论

暂无评论...
验证码 换一张
取 消