0

My problem seems trivial but after several hours of testing, researching and fiddling I can't seem to get this simple nginx rewrite function to work.

There are several rewrites we need, some will have multiple parameters but I cant even get this simple 1 parameter current url to alter at all to the desired.

Current: website.com/public/viewpost.php?id=post-title

Desired: website.com/public/post/post-title

Can someone kindly point me to as what I have done wrong, I am baffled / very tired...

For testing purposes before we launch we were just using a simple port on the server. Here is that section.

# Listen on port 7774 for dev test
server {
    listen          7774;
    server_name     localhost;
    root        /usr/share/nginx/html/paa;
    index       index.php home.php index.html index.htm /public/index.php;

location ~* /uploads/.*\.php$ {
    if ($request_uri ~* (^\/|\.jpg|\.png|\.gif)$ ) {
      break;
    }
        return 444;
    }


    location ~ \.php$ {
        try_files $uri @rewrite =404;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_pass php5-fpm-sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

location @rewrite { 
    rewrite ^/viewpost.php$ /post/$arg_id? permanent;
}

}

I have tried countless attempts such as above @rewrite and simpler:

location / {
    rewrite ^/post/(.*)$ /viewpost.php?id=$1 last;
}

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_pass php5-fpm-sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

I can not seem to get anything to work at all, I have tried changing the location tried multiple rules...

Please tell me what I have done wrong.

[relocated from stack overflow as per mod suggestion]

  • Your first code block tries to do opposite. Second one is closer to solution. – Alexey Ten Jun 11 '14 at 8:11
0

In you second code replace location with:

location /public/post/ {
    rewrite ^/public/post/(.+) /public/viewpost.php?id=$1;
}
  • I changed around the configuration as you had suggested but still no dice. When I load up the page it still doesn't perform any of the rewrites. – Olixr Jun 17 '14 at 6:07
  • What is the current config? – Alexey Ten Jun 17 '14 at 6:22
  • I had a silly error in the config. You solution worked. – Olixr Feb 29 '16 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.