Welcome! Log In Create A New Profile

Advanced

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

Posted by Joergi 
Hi guys,

I am new to nginx and I need to do a few rewrites, which I need help with.

I currently have this configuration:

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.

I have a root folder and inside it there is a folder called wiki/.

php5 files from inside one of these two folders should get rewritten to php
files.

E.g. the file /api.php5 should get rewritten to /api.php.
File /wiki/load.php5 should get rewritten to /wiki/load.php.
But file /wiki/some-subfolder/file.php5 should *not* get rewritten, because
it is neither in the root folder nor in wiki/ directly, but in one of the
subfolders of wiki. Files from subfolders should *not* get rewritten.

How can I get that?

Joerg

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,271450,271450#msg-271450

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
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.

f
--
Francis Daly francis@daoine.org
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Hi Francis,

thanks for your answer!

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

Yes, that is right. Files in other folders, also if the names of the files
end on php5, should not be rewritten.

> That is: no-slash, or /wiki/ then no-slash.
>
> rewrite ^/([^/]*)\.php5 /$1.php permanent;
> rewrite ^(/wiki/[^/]*\.php)5 $1 permanent;

I am currently on the way understanding what you are doing here. Pretty
sophisticated and way shorter than what I would have tried. In my tests this
is working as expected. Thanks for the input! :-)

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

What do you mean? The "main folder", which you are influencing with your
first rule, is the web root: /home/$username/www/. The foder wiki is the
subfolder in there. Any recommendation what location I should add around the
two rewrites or if I should add one?

Cheers!

Joerg

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,271450,271475#msg-271475

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
On Tue, Dec 13, 2016 at 01:29:17PM -0500, Joergi wrote:

Hi there,

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

> > You may want to restrict these to the locations that match their
> > prefixes, depending on what else is happening.
>
> What do you mean? The "main folder", which you are influencing with your
> first rule, is the web root: /home/$username/www/. The foder wiki is the
> subfolder in there. Any recommendation what location I should add around the
> two rewrites or if I should add one?

Depending on what else is in your config, it may be useful to put the
second rewrite within a "location ^~ /wiki/ {}" block.

But if you don't measure a difference when putting it there, in a
"location ~ \.php5 {}" block, or at server{] level, then it does not
matter in your deployment.

Cheers,

f
--
Francis Daly francis@daoine.org
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login