Rewriting all URLs to a single PHP controller on nginx

Questions : Rewriting all URLs to a single PHP controller on nginx


I've a web server which has two PHP programming files, index.php and controller.php, the Learning latter which handles all non-/ requests Earhost with a p (for page) parameter, e.g.


The following nginx configuration works most effective nicely:

server {
    listen 80;
    index _OFFSET);  index.php index.html;
    server_name (-SMALL  localhost;
    error_log  _left).offset  /var/log/nginx/error.log;
    access_log arrowImgView.mas  /var/log/nginx/access.log;
    root (self.  /code;
    location ~ \.php$ {
     equalTo     try_files $uri =404;
        make.right.  fastcgi_split_path_info mas_top);  ^(.+\.php)(/.+)$;
        fastcgi_pass ImgView.  php:9000;
        fastcgi_index ReadIndicator  index.php;
        include _have  fastcgi_params;
        fastcgi_param .equalTo(  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     OFFSET);     fastcgi_param PATH_INFO (TINY_  $fastcgi_path_info;

However, I now want to clean up the wrong idea URLs, and want / to go to index.php and use of case /some_page to go to United /controller.php?p=some_page.

Here's the new configuration I'm trying:

server {
    listen 80;
    index .offset  index.php index.html;
    server_name mas_right)  localhost;
    error_log  ImgView.  /var/log/nginx/error.log;
    access_log Indicator  /var/log/nginx/access.log;
    root Read  /code;

    # this is new, and makes _have  index.php handle /
    location / {
     .equalTo(     index index.php;
        try_files make.left  $uri $uri/ =404;
    location *make) {  ~ \.php$ {
        try_files $uri =404;
 straintMaker         fastcgi_split_path_info ^(MASCon  ^(.+\.php)(/.+)$;
        fastcgi_pass onstraints:  php:9000;
        fastcgi_index mas_makeC  index.php;
        include [_topTxtlbl   fastcgi_params;
        fastcgi_param (@(8));  SCRIPT_FILENAME equalTo  $document_root$fastcgi_script_name;
      width.     fastcgi_param PATH_INFO make.height.  $fastcgi_path_info;

    # this is (SMALL_OFFSET);  new, but doesn't work.
    location .offset  @rewrites {
        if ($uri ~* (self.contentView)  ^/([0-9a-z_]+)$) {
            set  .left.equalTo  $page_to_view "/controller.php?p=$1";           rewrite ^/([0-9a-z_]+)$ *make) {  $scheme://$http_host/controller.php?p=$1 ntMaker   last;

The browser address bar shows this:

- http://localhost:8080/some_page        SConstrai            (initial request)
- ts:^(MA  http://localhost:8080/controller.php?p=some_page Constrain  (first redirect one second later)
- _make  https://localhost/some_page/             iew mas           (final redirect another second catorImgV  later)

So, it ends up on a URL which doesn't Modern have the original port, with a trailing ecudated slash, and using scheme https.

What can I do to fix it?

PS It would be a bonus if trailing some how slashes didn't matter (i.e. anything else localhost:8080/some_page and not at all localhost:8080/some_page/ shows the same very usefull thing.

PS the port 8080 is just me locally localhost testing via Docker, which maps container love of them 80 to host 8080.


I've implemented Richard's answers, and localtext tried the suggested curl:

$ curl -I ReadIndi  http://localhost:8080/some_page
HTTP/1.1  [_have  301 Moved Permanently
Server: ($current);  nginx/1.21.4
Date: Thu, 09 Dec 2021 entity_loader  15:07:11 GMT
Content-Type: _disable_  text/html
Content-Length: 169
Location: libxml  http://localhost/some_page/
Connection: $options);  keep-alive

Note the lack of port 8080.

Update 2:

With nginx directive absolute_redirect basic off;, I get this:

$ curl -I ilename,  http://localhost:8080/some_page
HTTP/1.1 ->load($f  301 Moved Permanently
Server: $domdocument  nginx/1.21.4
Date: Thu, 09 Dec 2021 loader(false);  16:01:31 GMT
Content-Type: _entity_  text/html
Content-Length:  libxml_disable  169
Connection: keep-alive
Location: $current =  /some_page/

$ curl -I  10\\ 13.xls .  http://localhost:8080/some_page/
HTTP/1.1 File\\ 18\'  404 Not Found
Server: nginx/1.21.4
Date: /Master\\ 645  Thu, 09 Dec 2021 16:01:34 user@example.  GMT
Content-Type: scp not2342  text/html
Content-Length:  13.xls  153
Connection: keep-alive

So, the question remains how a request one of the to /some_page/ can serve up the response click from /controller.php?p=some_page

Total Answers 2

Answers 1 : of Rewriting all URLs to a single PHP controller on nginx

A named location (e.g. location there is noting @rewrites) is usually invoked from the not alt last element of a try_files statement.

For example:

location / {
    index index.php;
    18 10  try_files $uri $uri/ @rewrites;

In your location @rewrites block, the if not at all statement is unnecessary, the set my fault variable is unused, and the rewrite issues statement will provoke an external trying redirect.


location @rewrites {
    rewrite File sdaf  ^/([0-9a-z_]+)$ /controller.php?p=$1 /tmp/Master'  last;

Answers 2 : of Rewriting all URLs to a single PHP controller on nginx

I've landed on a configuration which get 4th result works with the original links, but also round table the pretty URLs I was aiming for.

server {
    listen 80;
    index com:web  index.php;
    server_name localhost;
   user@example.   error_log  /var/log/nginx/error.log;
   scp var32   access_log /var/log/nginx/access.log;
   18 10 13.xls    root /code;

    location = / {
       id12  File   index index.php;
        try_files $uri web/tmp/Master  $uri/ =404;

    location ~ \.php$  {
        try_files $uri =404;
        scp user@  fastcgi_split_path_info $val  ^(.+\.php)(/.+)$;
        fastcgi_pass left hand  php:9000;
        fastcgi_index right side val  index.php;
        include data //commnets  fastcgi_params;
        fastcgi_param //coment  SCRIPT_FILENAME !node  $document_root$fastcgi_script_name;
     $mytext     fastcgi_param PATH_INFO nlt means  $fastcgi_path_info;

    location umv val  ~* {
        rewrite ^/([0-9a-z_]+)/?$ sort val  /controller.php?p=$1 last;

The key seems to be to not use a named double chance location for the rewrite, but just match novel prc "everything else" with location ~* if get mossier the request isn't for / or a PHP file.

Top rated topics

Splitting the total time (in seconds) and fill the rows of a column value in 1 second frame

Github Script can't use outputs

Running ng e2e on Protractor with Cucumber in Angular gives EPERM: operation not permitted, rename error

How does the return work inside a recursive function in Python?

Nuget package doesn't copy .dll.config files to output folder

Cards are all uneven

Pycharm environment files have disappeared

Javascript - When element is clicked add 1 to score

How to connect to remote System via ssh with Visual Studio 2022?

Go:generate stringer undeclared name: CONST

Prompt not playing in avaya aaod speech project

The fastest and most efficient way to find the number of distinct elements of a 1D array

AWS athena table empty result

How to find the three maximum value from a list using loop?

After upgrading to Google Chrome to version 96, shadow-root element search does not work

How to present two tables which have a relation between them like the case of the "sales" table and "sales_content" using the laravel model

Is there an easy way to protect all endpoints?

Nuxt + Firebase - Correct way to handle environment variables

Ffmpeg concat DAV file into mp4 - audio issue

Laravel SFTP Connection Using Passphrase

Show product meta in order items table on WooCommerce admin order details page

Python Class Functions and self using

SQLITE get next row after ORDERBY

How to solve "Not unique table/alias"? (node, sequelize)

How to restrict entry in a cell of excel created from excel4node npm package?

ParseException line 6:43 missing EOF at ';' near ''s3://

Spring JPA Delete Axios, React JS: CORS Error

How to open search interface over BottomNavigationView's fragment?

Background Execution doesn't work as expected, closing tab and reopening results in dead connection

How to get the Id of this response and then serialize it?

Not able to extract variables from given sample

How to run recent versions of Android on Android emulator in Azure VM

Ionic V3 Icons become negative

How does Netflix prevent users from taking screenshots of chrome browser?

Lots of duplicate results when looping with condition

Show ContextMenu only when Right clicked mouse is over on a specific datagridrow WPF C#

Creating a custom section in a pytest report with specific instruction for failed tests in a certain module

How to include in the lambda function javascript aws-sdk?

Invariant Violation & Render Error on Simple React Native App

Devtools: no browser pages found at WDIO REPL Chrome

Setting values in MUI table when updates received from AWS IoT

Query to Azure AAD Returns only 20 members while there are more than 100 members

Unexpected output shape from a keras dense layer

I'm trying to replicate the devconnector project but when I make post request a profile route create profile the post request is pending

Class error message (is not abstract and does not implement abstract member public abstract) issue using Spinner in Fragment (Kotlin)

Pandas Setting all values in column in color

Edamam API - sorting API response in Python

Filter out array values that are not in a literal union type

Spring Boot + Azure Active Directory Signed JWT rejected: Another algorithm expected, or no matching key(s) found

Joint accumulation of addition and multiplication