How do I rewrite files, but only, if they are in one special folder?

Francis Daly francis at
Mon Dec 12 21:18:01 UTC 2016

On Sun, Dec 11, 2016 at 12:05:19PM -0500, Joergi wrote:

Hi there,

> location ~ \.php5 {
>     root /home/$username/www/;
>     rewrite ^/(.*)\.php5 /$1.php permanent;
> }
> The problem with this is that it rewrites the files, also if they are in
> subfolders - and this is what I do not want.

You don't say what you want to happen to those other files, so I will
leave it at "nothing special".

Just rewrite what you want to. That is: no-slash, or /wiki/ then no-slash.

  rewrite ^/([^/]*)\.php5 /$1.php permanent;
  rewrite ^(/wiki/[^/]*\.php)5 $1 permanent;

Untested, but it looks right :-)

You may want to restrict these to the locations that match their prefixes,
depending on what else is happening.

Francis Daly        francis at

More information about the nginx mailing list