Score:1

NGINX Rewrite Query as Path?

gb flag

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}

sv flag
Welcome to ServerFault. `if ($arg_post != "") { rewrite ^/posts/ /posts/$arg_post? permanent; }` is likely to work.
Nolan O avatar
gb flag
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 ?)
djdomi avatar
za flag
hows about to show, what url should be and is now shown, your comment doenst make it clearly enough
Nolan O avatar
gb flag
How PHP should see the URL: ``` /posts?post=12345 ``` And the End user should see ``` /posts/12345 ```
Score:0
sv flag

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;
        }
}
Nolan O avatar
gb flag
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" ```
sv flag
My mistake. Updated the answer now.
Nolan O avatar
gb flag
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?
sv flag
It may be possible. Please create a new question so that it could be seen by everyone for a possible answer.
Nolan O avatar
gb flag
actually, this post Solved it for me: (jusr replaced the ` location /directory ` with ` location /posts/ ` post link [here](https://serverfault.com/questions/767440/nginx-rewrite-rule-subfolder-404-error)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.