Skip to main content

Nginx rewrite rule examples



The content of the article:

Redirect to url with slash

Some CMS for sites may open pages with a slash at the end, and without it, if the engine does not redirect itself. For example, the page http://example.net/page and http://example.net/page/ from the point of view of the visitor will be one. But for search engines, these are two different pages, and slash is to blame for everything. So duplicate content is bad.

To redirect to the slash page, add the following line to the nginx configuration file for your site:


  rewrite ^ ([^.] * [^ /]) $ $ 1 / permanent; 

Now the page without a slash will be redirected to the 301th redirect to the page with the slash. For pages with extensions (.html, .php, etc.), this rule will not affect. In addition, it is universal and, so that the engine does not process redirects using php, this rule should be prescribed for most sites where human-readable addresses are used.

Rewrite for WordPress

The WordPress documentation has a nice example of rewrite rules. I will just highlight the most basic things that are required to work in conjunction with php-fpm

  location / {
   try_files $ uri $ uri / /index.php$args;
  }
  location ~ \ .php $ {
   try_files $ uri = 404;
   fastcgi_pass unix: /run/php-www.sock;
   fastcgi_index index.php;
   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
   fastcgi_param PHP_VALUE "sendmail_path = / usr / sbin / sendmail -t -i -fmail@example.com";
   fastcgi_param PHP_ADMIN_VALUE "open_basedir = / var / www / example.com /: / var / save_path /: / var / tmp_dir /";
  } 

Rewrite for MODx

For sites on this CMS, I use the following rules:


  location / {
 try_files $ uri $ uri / @rewrite;
 }
 location @rewrite {
 rewrite ^ / (. *) $ /index.php?q=$1;
 }
 location ~ \ .php $ {
  try_files $ uri = 404;
  fastcgi_pass unix: /run/php-www.sock;
  fastcgi_index index.php;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
  fastcgi_param PHP_VALUE "sendmail_path = / usr / sbin / sendmail -t -i -fmail@example.com";
  fastcgi_param PHP_ADMIN_VALUE "open_basedir = / var / www / example.com /: / var / save_path /: / var / tmp_dir /";
 } 

301st redirect from index.php, index.html in any folder

It also happens that the CMS itself does not redirect from index files to the url without them. For example, the site becomes available both at http://example.com/ and http://example.com/index.php. This rule corrects the error.

  if ($ request_uri ~ * index. (php | html)) {
   rewrite ^ (. +) index.php $ scheme: // $ host $ 1 permanent;
 } 

In this case, it does not matter if the index file is in the root folder or in the subfolder, the redirect will always work.

Other

I have one of his blog decided to change the structure of links. Previously, all posts were accessible via links, such as / postname.html, now they have moved to another address: / fotojournal / postname /. Initially, as a solution, I thought about using the redirection plugin for wordpress, but then I wanted to try to implement it on nginx. And the idea turned out. :)



True, the rule turned out to be simple, due to the fact that all the pages /%postname%.html were transferred to one category - fotojournal. This rule looks like this:

  location ~ * \. (html) $ {
   try_files $ uri $ uri / @fotojournal;
 }

 location @fotojournal {
   rewrite ^ / (. *) \. html $ / fotojournal / $ 1 / permanent;
 }

We consider in detail. When a request comes from a user, first any file with the .html extension is searched for in the folder with the site. This is necessary for real-life html-pages on the site, which should not be transferred to a new category.

If the requested page was not found, the request is redirected to the second location - @fotojournal. Here you are redirected (redirect 301) to a new address. For example, page /page.html opens at / fotojournal / page /.



How do you rate the article?
Звёзд: 1Звёзд: 2Звёзд: 3Звёзд: 4Звёзд: 5 ( 10 ratings, average: 2.70 out of 5)
Loading...

Add a comment

Your email will not be published.