Rewrite

Hello,

i want use rewrite to use article-“id”.php to the post.php?id=1 for
example.
I has written the follow in the server section:

location /article-(\d+).php$ {
try_files $uri $uri/ /post.php?id=$1;
}

But it want not run. When i give http://example.com/blog/article-1.php,
nginx not change to http://example.com/blog/post.php?id=1

192.168.0.20 - - [20/Apr/2012:01:52:16 +0200] “GET /blog/article-1.php
HTTP/1.1”
404 27 “http://silviosiefke_de/blog/pdo-read.php” “Mozilla/5.0 (Windows
NT 5.1)
AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162
Safari/535.19” “-”

Has someone idea whereis the mistake?

Thanks for help.

Regards
Silvio

2012/4/20 Silvio S. [email protected]:

nginx not change to http://example.com/blog/post.php?id=1

  1. you forgot to put ~ for regex match
  2. the one you want is rewrite/return, not try_files

something like this may work:

location ~ ^/article-(\d+).php$ {
rewrite ^ /post.php?id=$1 last;
}

On Fri, 20 Apr 2012 06:56:51 +0700
Edho A. [email protected] wrote:

  1. you forgot to put ~ for regex match
  2. the one you want is rewrite/return, not try_files

Thanks for help. So i have from this list, with the html files it run.
But nginx want not use html inside php so i must change the rule.

something like this may work:

location ~ ^/article-(\d+).php$ {
rewrite ^ /post.php?id=$1 last;
}

Is the same, i become “File not found”.

192.168.0.20 - - [20/Apr/2012:02:40:32 +0200] “GET /blog/article-1.php
HTTP/1.1” 404 27 “http://silviosiefke_de/blog/index.php” “Mozilla/5.0
(Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko)
Chrome/18.0.1025.162 Safari/535.19” “-”

Regards
Silvio

gentoo-desk db # nginx -V
nginx version: nginx/1.0.15
TLS SNI support enabled
configure arguments: --prefix=/usr --sbin-path=/usr/sbin/nginx
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error_log
–pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock
–with-cc-opt=-I/usr/include --with-ld-opt=-L/usr/lib
–http-log-path=/var/log/nginx/access_log
–http-client-body-temp-path=/var/tmp/nginx/client
–http-proxy-temp-path=/var/tmp/nginx/proxy
–http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
–http-scgi-temp-path=/var/tmp/nginx/scgi
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
–with-http_addition_module --with-http_dav_module
–with-http_realip_module
–with-http_stub_status_module --with-http_sub_module
–with-http_xslt_module
–with-http_realip_module --with-pcre
–add-module=/var/tmp/portage/www-servers/nginx-1.0.15/work/agentzh-headers-more-nginx-module-137855d
–add-module=/var/tmp/portage/www-servers/nginx-1.0.15/work/nginx_http_push_module-0.692
–add-module=/var/tmp/portage/www-servers/nginx-1.0.15/work/nginx_upload_module-2.2.0
–with-http_ssl_module --without-mail_imap_module
–without-mail_pop3_module
–without-mail_smtp_module --user=nginx --group=nginx

2012/4/20 Silvio S. [email protected]:

location ~ ^/article-(\d+).php$ {
rewrite ^ /post.php?id=$1 last;
}

Is the same, i become “File not found”.

Put the block before all other locations.

Hello,

On Fri, 20 Apr 2012 06:56:51 +0700
Edho A. [email protected] wrote:

  1. you forgot to put ~ for regex match
  2. the one you want is rewrite/return, not try_files

something like this may work:

location ~ ^/article-(\d+).php$ {
rewrite ^ /post.php?id=$1 last;
}

Ok i have it do, but the result is same, File not found.

The config complete: Loading...

Regards
Silvio