Question

NGINX simple rewrite problem

  • Posted October 18, 2014

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.

Subscribe
Share

Try replacing

    location / {
        try_files $uri $uri/ =404;

        rewrite ^/(.*)/$ /?page=$1 last;
        #rewrite ^/(.*)$ $1/ permanent;
    }

with

    location / {
        try_files $uri $uri/ =404;

        rewrite ^/(.*)/$ /?page=$1 last;
        rewrite ^(.*[^/])$ $1/ permanent;
    }

and restarting nginx. Does that fix it? Make sure you clear your browser’s cache before testing it.

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!

server {
     ... things ....
     rewrite ^(.*[^/])$ $1/ permanent;
     location / {
         try_files $uri $uri/ =404;
     }
}

Okay, it works now! I needed to add it like you said kamaln7. Thanks to you you to vegardx. :)

I have restarted it and it did not work kamaln7.

Here is my default config as it is right now:

       server {
	listen 80;
	listen [::]:80;
	server_name damircalusic.se www.damircalusic.se;
	
	add_header Strict-Transport-Security max-age=15768000;
	return 301 https://www.damircalusic.se$request_uri;
     }
     server {
      listen 443 ssl;
      server_name damircalusic.se;

    ssl_certificate /etc/nginx/ssl/SSL.crt;
    ssl_certificate_key /etc/nginx/ssl/KEY.key;

    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;
    ssl_prefer_server_ciphers on;

    return 301 https://www.damircalusic.se$request_uri;
    }
    
    server {
	listen 443 ssl;
        server_name www.damircalusic.se;
	
	root /var/www/damircalusic.se/html;
	index index.php index.html index.htm;
	
	ssl_certificate /etc/nginx/ssl/SSL.crt;
     ssl_certificate_key /etc/nginx/ssl/KEY.key;
	
	ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;
	ssl_prefer_server_ciphers on;

	error_page 403 /error/403.html;
	error_page 404 /error/404.html;
	
	charset utf-8;
	
	#allow 83.253.180.90;
	#deny all;
	
	if ( $request_uri ~ ^(/index\.php)$ ) {
        return 301 https://www.damircalusic.se;
     }
		
	location / {
		try_files $uri $uri/ =404;
		
		rewrite ^/(.*)/$ /?page=$1 last;
		#rewrite ^/(.*)$ $1/ permanent;
	}

	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;
	}
	
	location ~ /\.ht {
		deny all;
	}
	
	location = /favicon.ico {
		log_not_found off;
		access_log off;
	}

	location = /robots.txt {
		allow all;
		log_not_found off;
		access_log off;
	}
	
	location ~ /\. { 
		deny all; 
		error_log off; 
		log_not_found off; 
	}
	
	location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
		log_not_found off;
		expires 365d;
		add_header Cache-Control "public, max-age=315360000";
	}
	
	location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|png|gif|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
		access_log off;
		log_not_found off;
		expires max;
		add_header Pragma public;
		add_header Cache-Control "public, must-revalidate, proxy-revalidate";
	}
	
	location ~* \.(7z|ai|class|css|csv|ejs|eps|flv|html?|jar|jpe?g|js|json|lzh|m4a|m4v|mov|mp3|pdf|pict|pls|ps|psd|swf|tiff?|txt|webp)$ {
		access_log off; 
	   	log_not_found off;
		expires max;
		add_header Pragma public;
		add_header Cache-Control "public, must-revalidate, proxy-revalidate";
	}
	
	location = /error/403.html {
		root /var/www/damircalusic.se/html;
		allow all;
    }
	
	location = /error/404.html {
		root /var/www/damircalusic.se/html;
		allow all;
    } 
    }

Have you restarted nginx after modifying the config file? If yes, can you post the all of the file’s contents?

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?


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

It works perfectly now thank you kamaln7 and vegardx. :)