• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Need help convert apache rewrite rules to nginx

Nerijuss

New Pleskian
Hello,
please help me convert apache rewrite rule to nginx:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^naujienos+ news.php/$1 [L]
RewriteRule ^naujienos/p(.*)$ /news.php?pg=$1 [nc]
RewriteRule ^naujienos/?$ /news.php [nc]
RewriteRule ^naujienos?$ /news.php [nc]
 
Hi Nerijuss,

You can try this converter - http://winginx.ru/htaccess

Then you will need to put nginx directives into custom nginx vhost config. However, please do not touch the config, created by Plesk Panel.
You should add your custom nginx directives into Plesk nginx vhost template -
/usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php

Then save it as a custom template -
/usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php
 
Can you be more detailed? for example:

these are the default rewrite rules of a standard wordpress:

Code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
translated with that tool it transform to:
Code:
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}
but is not applicable in plesk vhost section as it complain about a duplicate "/" section.

I tried/found also with the rule:
Code:
if (!-e $request_filename) { rewrite ^(.*)$ /index.php break; }
and it's work...but it broke for example the access to webstatics, (seems that the section location ~ ^/plesk-stat/) is ignored/ovverridden by that rewrite rule.

ideas?

Thanks

Matteo
 
sgala.matteo,

Since WordPress configuration could be a quite challenging task, I cannot give you advises for the rewrite rules itself.

I suggest you to look into Nginx documentation:
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
http://nginx.org/en/docs/http/ngx_http_core_module.html#location

As an idea - you can create a special location for Domain.tld/plesk-stat/ folder, without any rewrite rules processing.

As another option, you can think of upgrade to Plesk 12. It has built WordPress toolkit that simplifies WP management: updates, plugins, themes:
http://download1.parallels.com/Plesk/Doc/en-US/online/plesk-administrator-guide/73563.htm#
 
Hi Alexey, I know where the documentation is located. The question of Nerijuss and mine was more technical and precise.

I know that WP is provided also by Plesk automated installer but I'm not forced to use it and this is not related to this topic.

The question is very precise: how to convert these apache rewrite rules to nginx rewrite rules as Plesk complain for / location already defined.

The question it's very precise and related to Plesk, the NGINX documentation can't give us a solution but only workaround to bypass the Plesk error.

Can you clarify how to write a correct rule that doesn't broke Plesk and it's in a "plesk way". I'm sure that a senjor engineer at Parallels can answer this.

Thanks a lot,

M.
 
sgala.matteo,

Switching to technical mode: correct, "location /" is already in nginx config, created by Plesk. Thus you cannot define it again.

However, one do not need to specify the root location explicitly, the code below must work for all locations:
if (!-e $request_filename){ rewrite ^(.*)$ /index.php break; }

In order to alter nginx behavior for some location, one should add specific rules for this location (plesk-stat, as example).

 
Back
Top