Rewrite rules not working

B
  • 10 Nov '22
Hi Team,

I am trying to write a below rewrite rule but somehow this is not working
and I would really appreicate if someone can help me on this?

I have a website http://web1.example.local/web1
Instead I need a rewrite so that if user enters http://web1.example.local it
will be diverted to http://web1.example.local/web1

server {
        listen 80;
        server_name  web1.example.local;
        add_header X-Frame-Options "SAMEORIGIN";
        access_log /var/log/nginx/web1/access.log;
        error_log /var/log/nginx/web1/error.log;
        rewrite ^ http://web1.example.local

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,295715,295715#msg-295715
F
  • 11 Nov '22
On Thu, Nov 10, 2022 at 01:07:23PM -0500, blason wrote:

Hi there,

> I have a website http://web1.example.local/web1
> Instead I need a rewrite so that if user enters http://web1.example.local it
> will be diverted to http://web1.example.local/web1

If you want it to happen, without needing it to be a rewrite, you can
do a redirect with

    location = / { return 301 /web1; }

(although I suspect that you will want a trailing slash there,
"/web1/;". And variants with a different http response code can be
used. And you can use the full "http://web1.example.local/web1/" if
you prefer.)

Cheers,

    f
-- 
Francis Daly        francis at daoine.org
B
  • 11 Nov '22
Thanks appreciate it. Will have to check and confirm.

By the way which one would you confirm is preferable method rewrite or
return?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,295715,295725#msg-295725
F
  • 11 Nov '22
On Fri, Nov 11, 2022 at 08:29:44AM -0500, blason wrote:

Hi there,

> By the way which one would you confirm is preferable method rewrite or
> return?

It depends, based on what you want to do.

For what I think you want, in this case, "return" is simpler.

    f
-- 
Francis Daly        francis at daoine.org