Welcome! Log In Create A New Profile

Advanced

nginx rewrite help/question

Posted by jelson41 
nginx rewrite help/question
August 28, 2012 04:28PM
Hi,
We are trying to rewrite specific pages to specific directories and it isn't working properly. I was wondering if somebody could assist.

Here is an example of what we are trying to:

#1 This doesn't work when redirecting to a directory (/tech):
rewrite ^/tech/index.shtml$ http://www.site.com/tech/ permanent;

#2 This does work when redirecting to a specific file (index2.shtml)
rewrite ^/tech/index.shtml$ http://www.site.com/tech/index2.shtml permanent;

I want to know how we can do option #1. Any help is greatly appreciated it!
Re: nginx rewrite help/question
August 28, 2012 05:50PM
Avoid rewrites:

location ~ ^(/tech/.*)$ {
return 301 http://www.site.com$1;
}
Re: nginx rewrite help/question
August 29, 2012 10:43AM
Why should we avoid rewrites? We literally have hundreds of these we need to do and have already done hundreds of others that include file names. Thanks in advance!
Re: nginx rewrite help/question
August 29, 2012 12:21PM
Because if you replace all rewrites with "location" blocks, you will no longer require rewrite module at all, unless you also use "if"s (see "if is evil" page) or "return"s. According to developers, location directive is slightly faster, and if you have hundreds of them, then you may assume performance increase.

I offered you the solution that does not require rewrite. The same can be applied to all hundred rewrites that you already have, but it's your choice.

Good luck and kind regards,

Andrew
Re: nginx rewrite help/question
August 29, 2012 12:25PM
Thanks for the information Andrew. I really appreciate it and will look into that. Is there no way to use the "rewrite" option to accomplish what I'm trying to do while I investigate the "location" option?
Re: nginx rewrite help/question
August 29, 2012 12:29PM
Also, wont the example you posted redirect all pages at /tech/ to /tech even if we have 50 different files in that directory? We only want to redirect the one file index.shtml2
Re: nginx rewrite help/question
August 29, 2012 12:32PM
AFAIK (from my experience), just about ANY rewrite can be replaced with appropriate location block. Note that rewrites for static links, such as:

rewrite ^/static/folder$ http://www.mysite.com/static/folder permanent;

can be replaced with exact match location query (even slightly faster than regex location match) :

location = /static/folder {
return 301 http://www.mysite.com/static/folder;
}

Of course,

Andrejs
Re: nginx rewrite help/question
August 29, 2012 12:35PM
jelson41 Wrote:
-------------------------------------------------------
> Also, wont the example you posted redirect all pages at /tech/ to
> /tech even if we have 50 different files in that directory? We only
> want to redirect the one file index.shtml2

No problem:

location ~ ^(/tech/)index\.shtml$ {
return 301 http://www.site.com$1;
}

Andrejs



Edited 1 time(s). Last edit at 08/29/2012 12:35PM by locojohn.
Re: nginx rewrite help/question
August 29, 2012 01:40PM
That doesn't redirect for me either.
Re: nginx rewrite help/question
August 29, 2012 02:13PM
Nevermind, I got this figured out. It was working all along but the pages were being cached by the boost module for drupal. Thanks for the help!
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 82
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready