1

What i am trying to do:

Convert

/posts/?post={randomnumber-atoF}
or
/posts?post=120430awasdfwasfw

to

/posts/120430awasdfwasfw

my Server Block (so far)

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /forum;
        index index.php;
        server_name forum.example.net;
        location / {
                try_files $uri $uri/ =404;
        }
        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

The server is getting the URL passed from another Nginx Proxy (another remote server). (and this server does have SSL, port 443)

Items i have tried:

rewrite ^/posts/index.php /posts/$arg_post permanent;

edit:

sorry i didnt say, the query most go into the Index, as the ?post is a query, into a database. I want to make it so you dont need the query, but just go to the {post number}

4
  • Welcome to ServerFault. if ($arg_post != "") { rewrite ^/posts/ /posts/$arg_post? permanent; } is likely to work. Jul 7, 2021 at 7:26
  • i tried your code above, and it comes to a 404 (if the post={post number} is not found in the DB, it returns 404) Is there a way to rewrite the URL while keeping the /posts/?post=333 (act like? without the ?)
    – user655355
    Jul 7, 2021 at 7:31
  • 1
    hows about to show, what url should be and is now shown, your comment doenst make it clearly enough
    – djdomi
    Jul 7, 2021 at 8:01
  • How PHP should see the URL: ``` /posts?post=12345 ``` And the End user should see ``` /posts/12345 ```
    – user655355
    Jul 7, 2021 at 8:07

1 Answer 1

0

The title of the post could be "convert / rewrite URL path to query". Anyway, as per the edit and the comments, rewrite ^/posts/(?<id>[a-zA-Z0-9]+)$ /posts/?post=$id; may work. No need to use permanent flag for rewrite. If you use permanent, the user will be redirected.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /forum;
        index index.php;
        server_name forum.example.net;

        rewrite ^/posts/(?<id>[a-zA-Z0-9]+)$ /posts/?post=$id;

        location / {
                try_files $uri $uri/ =404;
        }
        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}
5
  • apon running it, i get this error: ``` 2021/07/07 08:51:19 [error] 244184#244184: 4 rewrite or internal redirection cycle while internally redirecting to "/posts/index.php", client: .*.**.***, server: forum.example.net, request: "GET /posts/?1318131055449140129 HTTP/1.1"> </1.1", host: "192.168.0.195" ```
    – user655355
    Jul 7, 2021 at 8:53
  • My mistake. Updated the answer now. Jul 7, 2021 at 9:16
  • is there a way to make the Rewrite only if the file isnt found? like if ``` /posts/file``` goes to that file, but if /posts/file {error code 404} then try the rewrite?
    – user655355
    Jul 8, 2021 at 10:00
  • It may be possible. Please create a new question so that it could be seen by everyone for a possible answer. Jul 8, 2021 at 10:36
  • actually, this post Solved it for me: (jusr replaced the ` location /directory ` with ` location /posts/ ` post link here
    – user655355
    Jul 8, 2021 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .