Report this

What is the reason for this report?

How do I convert this .htaccess snip to Nginx?

Posted on March 28, 2022

I have this snip of .htaccess:

RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %{DOCUMENT_ROOT}/u_pages/%1.php -f
RewriteRule .* u_pages/%1.php? [L]

Using an online converter I get this:

location / {
  if ($request_filename ~ index\.php){
    rewrite ^(.*)$ /u_pages/%1.php? break;
  }
}

-Doesn’t work.

What I’m trying to do:

I have a bunch of plain .php documents in /u_pages – e.g. 123.php

I need these to have URLs like this:

example.com/index.php?id=123

The document NAME corresponds with the number after id=

Any help is greatly appreciated, thank you.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @ ,

You can try the following redirect:

location ^~ /u_pages {
    return 301 /index.php%1.php;
}

Not really sure but it should work.

Or with rewrite, the query string is automatically appended unless a ? is added:

rewrite ^/u_pages/$ /index.php$1 permanent;

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Dark mode is coming soon.