Image

Knowledge base → SEO URL optimization in nginx

[Virtual servers]
Date of publication: 09.06.2023

For search engines, it is important that the site does not have duplicate pages, as this has a bad effect on the ranking of content and the perception of pages by search bots. When hosting a site, it is necessary to check the link options for which the same page will be available at different addresses, which must be fixed using a 301 (moved permanently) redirect. Consider the most popular examples.


1. Setting up a redirect when visiting a page with a www prefix, for example https://www.domain.tld, we will redirect without www i.e. https://domain.tld

if ($host ~* ^www.domain.tld$) {

rewrite ^(.*)$ https://<mark>domain.tld$1 permanent;

}


2. Setting up a redirect without / at the end of the url address, for example https://domain.tld/ let's redirect without / i.e. https://domain.tld and all other pages like https://domain.tld/my-page/ in https://domain.tld/my-page

location ~ ^(.+)/$ { 

return 301 $1$is_args$args;

}


3. Often the same page is available at https://domain.tld/ and https://domain.tld/index.php, as well as all other pages https://domain.tld/my-page and https:/ /domain.tld/index.php/my-page. Remove index.php from all url addresses

if ($request_uri ~* "^(.*/)index\.php(/?)(.*)") {         

return 301 $1$3;

}


These settings must be applied in the server {} block. After making changes, restart the nginx service with the command:

service nginx restart




No Comments Yet