T
T
tmitry2014-01-03 16:10:43
PHP
tmitry, 2014-01-03 16:10:43

How to beautifully write rewrite .php file so that it is executed before the location responsible for fcgi processing?

In the process of transferring .htaccess to the nginx config, I encountered a problem: You
need to rewrite from example.com/a.php to example.com/b.php

location / {
   rewrite ^/a.php$   b.php;
}

location ~ \.php$ {
  fastcgi_pass  localhost: 9000;
  fastcgi_param SCRIPT_FILENAME
                      $document_root$fastcgi_script_name;
  include fastcgi_params;
}

The rewrite above doesn't redirect to b.php. After reading the documentation, I found out that a location with a regular expression is more priority, respectively, example.com/a.php immediately gets into the location for fcgi processing. But it entered into a stupor why the following rewrite works, although it also has a lower priority:
location /a.php {
   rewrite ^/a.php$   b.php;
}

location ~ \.php$ {
  fastcgi_pass  localhost: 9000;
  fastcgi_param SCRIPT_FILENAME
                $document_root$fastcgi_script_name;
  include fastcgi_params;
}

PS: Share your experience on how to beautifully write rewrite .php file so that it is executed before the location responsible for processing fcgi.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Sheleg, 2014-01-03
@Duti_Fruti

Try inserting a rewrite into the main location.

K
kompi, 2014-02-04
@kompi

The longest match takes precedence, then other rules come into play.

location /a.php {
    return 301 http://example.com/b.php;
}

S
sinyawskiy, 2014-10-02
@sinyawskiy

It's still possible like this

if ($request_uri = /a.php) {
         rewrite ^ http://$host/b.php? permanent;
}

permanent to have a 301 redirect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question