Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Nginx custom redirect
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Nginx custom redirect

I'm hitting the wall with this one, I have an html site on a VPS with Nginx and I want to rewrite shop.html to be just shop (I don't want the rewrite to affect my other html pages).

Anything I try though fails and i'm guessing that i'm doing something wrong.

I've been trying to customize this code snippet here so that I make it only apply on the specific page rather than all html pages but i'm unsure of how to customize it correctly to only apply on shop.html

location ~ { if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; } try_files $uri $uri/ $uri.html $uri.php?$args; }

Comments

  • As far as I know nginx searches for the most specific location first, so couldn't you just add another location directive regarding the shop.html to your config file?

  • TWoTWo Member

    location / { try_files $uri $uri/ $uri.html $uri.php?$args; } location = /shop.html { return 302 /shop; }

  • Rewrite or redirect? If you want the visitor to see /shop while Nginx serves shop.html then something like this should work:

    location /shop {
        alias $document_root/shop.html;
    }
    

    If you want the visitor who lands on /shop.html to be redirected to /shop go with what @TWo posted.

  • TWoTWo Member

    @JustAMacUser Better use "location = /shop" - your snippet may have side effects as it will serve /shop.html's content also for requests to /shopping, /shopwhatever and /shop.html

  • I considered that but wanted to account for use such as /shop/. A regex is probably best in this situation unless the OP has very specific needs. I was mostly just going for another method depending on what the desired end result needed to be. Thanks for pointing this out as it will no doubt be useful to know.

  • zafouharzafouhar Veteran
    edited October 2016

    Thanks guys, I tried all your suggestions and a few more - none work on my config (they do work on a completely fresh Nginx install), though that is likely due to some other issue with my Nginx file - I either get redirect loops or Nginx fails to start. I'll look into this further as to what is causing the issue

  • TWoTWo Member

    Mind sharing your config? Most probably location lines might be sufficient. Nginx is a bitch about priority of location lines.

  • @TWo said:
    Mind sharing your config? Most probably location lines might be sufficient. Nginx is a bitch about priority of location lines.

    Yeah indeed, its some location lines that I put - when I remove all my customization's it works like a charm

Sign In or Register to comment.