1

I have an URL like ^/d/something/(.*)$, what I want to do is rewrite it internally, with no permanent or temporary HTTP redirect, so that $_SERVER['REQUEST_URI'] will have just $1 and not the whole ^/d/something/(.*)$. Using nginx.

Here is what I tried:

location /d/something/ { rewrite "^/d/something/(.*)$" /$1 last; } location / { include php-fcgi.conf; try_files $uri $uri/ /index.php$args; } 

in the php-fcgi.conf there is fastcgi_param REQUEST_URI $request_uri; and $request_uri remains the same, so when I hit /d/something/something2 symfony reouter, which relies on $_SERVER['REQUEST_URI'] shows /d/something/something2 while I expect it to be just /something2. I assume that's because $request_uri is not changed.

If I replace it with fastcgi_param REQUEST_URI $uri; then $_SERVER['REQUEST_URI'] becomes / , no matter what I send in something2 part, it's always just /. Why is that happening and how can I internally rewrite it to /$1?

Thanks!

UPDATE: here are the contents of php-fcgi.conf:

location ~ \.php { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 60s; } 
1
  • What does php-fcgi.conf contain? Commented Aug 21, 2019 at 10:19

1 Answer 1

1

I would expect $uri to have the value /index.php by the time the request is passed to the PHP script, so I don't understand why you are seeing /. However...

The simplest solution would be to execute the PHP script from within the location where the URI is rewritten. This is achieved using a rewrite...break and overwriting the REQUEST_URI and SCRIPT_FILENAME parameters.

For example:

location /d/something/ { rewrite "^/d/something/(.*)$" /$1 break; include fastcgi_params; fastcgi_param REQUEST_URI $uri; fastcgi_param SCRIPT_FILENAME $document_root/index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 60s; } 

Place the fastcgi_param statements after the include statement.


Alternatively, use a regular expression location block. Note that the order of regular expression location blocks is significant. See this document for details.

For example:

location ~ ^/d/something(/.*)$ { try_files /index.php =404; include fastcgi_params; fastcgi_param REQUEST_URI $1; fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 60s; } 
1
  • Thanks Richard! I just tested the first approach and it worked. I think the main problem with my attempts were that I modified REQUEST_URI, I should have replaced SCRIPT_FILENAME and use break , but then pointing it to php-fcgi as it will never reach the second pass. Thanks again! Commented Aug 24, 2019 at 5:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.