Question
NGINX simple rewrite problem
Hi,
I have a simple rewrite problem with my website https://www.damircalusic.se.
I would like to have a permanent rewrite:
From | To |
---|---|
damircalusic.se/om | damircalusic.se/om/ |
My config looks like this:
location / {
try_files $uri $uri/ =404;
rewrite ^/(.*)/$ /?page=$1 last;
#rewrite ^/(.*)/$ /$1/ permanent;
}
When I uncomment rewrite 2 it becomes an endless loop of redirect.
How do I achieve this rewrite?
Regards.
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×
The loop is pretty simple to explain, just look at what you’re essentially doing. First you’re matching against try_files, then you rewrite that. A rewrite will give the user a 301 or 302, depending on what you’re doing, making you do the entire request again.
In order to fix this, rewrite all URLs to add a tailing slash before your location-block. I’m also unsure what you’re trying to achieve with that rewrite.
I haven’t tried this, but I’m pretty sure it should work. Also, this sounds like some SEO non-sense!
It did not work vegardx. If you have anymore suggestions feel free to speak because I am listening.
How would you achieve these rewrites? I am new to NGINX so I am listening. You said that I am matching against try_files then I am rewritinging that. Is that the wrong way to do that? How would you go about and make these rewrites?
Have you restarted nginx after modifying the config file? If yes, can you post the all of the file’s contents?
I have restarted it and it did not work kamaln7.
Here is my default config as it is right now:
Try replacing
with
and restarting nginx. Does that fix it? Make sure you clear your browser’s cache before testing it.
Okay, it works now! I needed to add it like you said kamaln7. Thanks to you you to vegardx. :)