Welcome! Log In Create A New Profile

Advanced

rewriting only if file exists

Posted by pmcgee 
rewriting only if file exists
February 15, 2010 10:22PM
Hi, I'm having problems with setting up a rewrite.
I basically want any POSTs or cache misses to go to index.php
My biggest problem is I want to parse an ID from the url and check if it exists in the cache

[code]
if ($uri ~* "([0-9]+).*") {
rewrite ([0-9]+).* /cache/$1.html;
break;
}
[/code]

not really sure how to check if it exists before rewriting.
here's my full location block:


[code]
location / {
index index.php;

if ($request_method = POST) {
rewrite ^(.*)$ /index.php last;
break;
}

if (-f $document_root/cache/$uri/index.html) {
rewrite (.*) /cache/$1/index.html;
break;
}

if (-f $document_root/cache/$uri) {
rewrite (.*) /cache/$1;
break;
}

if ($uri ~* "([0-9]+).*") {
rewrite ([0-9]+).* /cache/$1.html;
break;
}

if (!-f $request_filename) {
rewrite ^(.*)$ /index.php last;
break;
}

if (!-d $request_filename) {
rewrite ^(.*)$ /index.php last;
break;
}
}
[/code]
Re: rewriting only if file exists
March 08, 2010 02:50PM
figured this out with using variables

[code]
location / {
index index.php;

if ($request_method = POST) {
rewrite ^(.*)$ /index.php break;
}

if (-f $document_root/cache/$uri/index.html) {
rewrite (.*) /cache/$1/index.html break;
}

if (-f $document_root/cache/$uri) {
rewrite (.*) /cache/$1 break;
}

set $cache_file '';

if ($uri ~* "([0-9]+).*") {
set $cache_file /cache/$1.html;
}

if (-f $document_root$cache_file) {
rewrite ^(.*)$ $cache_file break;
}

if (!-e $request_filename) {
rewrite ^(.*)$ /index.php last;
break;
}
}
[/code]
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 317
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