Question

How do I convert this .htaccess snip to Nginx?

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.


Submit an answer
Answer a question...

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!

Sign In or Sign Up to Answer

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.

KFSys
Site Moderator
Site Moderator badge
March 29, 2022

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;