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;
}
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 arewrite...break
and overwriting the REQUEST_URI and SCRIPT_FILENAME parameters.For example:
Place the
fastcgi_param
statements after theinclude
statement.Alternatively, use a regular expression
location
block. Note that the order of regular expressionlocation
blocks is significant. See this document for details.For example: