Question

How to add customized rewrite rules to branding configuration?

Answer

By default, platrom does not support adding rewrite rules (for customization purposes) to branding configuration manually.

Platform do not support manually configured rewrite rules for stores (e.g. through editing ".htaccess" file by vim) and support manually edited files can lead us to error-prone heuristics . But for this situation we can use a script that allows to make the necessary changes so that they become known to the platform. With custom_rewrite_rules.py it's possible to add or remove custom rewrite rule through API.

You will need to place this script on MN and call it for all branded stores with custom rewrite rules, once for each brand. For each call you needs to specify:

a) operation (add or remove rewrite rule)

b) brand_id (Brand Id. Can be found in brands list: System -> Settings -> Brands)

c) file (the file with all custom Store Rewrite Rules for brand, like rules.txt)

The contents of the file with rules will be inserted into the resulting ".htaccess" during the upgrade and it will be inserted AS IS.

You need to build this file by yourself, and this file must contain all your own manual overwriting rules for stores on this brand. (Please look at attached "rules.txt" for example.)

Changes will be immediately reflected on brand ".htaccess" file (through "Update configuration for brand" task). BACKUP YOUR ORIGINAL "htaccess" files

Example of usage:

$ python custom_rewrite_rules.py -h
usage: custom_rewrite_rules.py [-h] operation brand_id file

Add/Remove custom rewrite rules for store

positional arguments:
operation   add/remove rewrite rule
  brand_id    brand ID
  file        the file with Rewrite Rules for store

optional arguments:
  -h, --help  show this help message and exit

$ python custom_rewrite_rules.py add 4 rules.txt
2019-11-06 15:40:59.020 [DEBUG] read agent config from /usr/local/pem/etc/pleskd.props
2019-11-06 15:40:59.020 [DEBUG] read kernel config from /usr/local/pem/etc/Kernel.conf
2019-11-06 15:40:59.021 [DEBUG] read odbc config from /usr/local/pem/etc/odbc.ini
2019-11-06 15:40:59.021 [DEBUG] read agent config from /usr/local/pem/etc/pleskd.props
2019-11-06 15:40:59.021 [DEBUG] read kernel config from /usr/local/pem/etc/Kernel.conf
2019-11-06 15:40:59.022 [DEBUG] read odbc config from /usr/local/pem/etc/odbc.ini
2019-11-06 15:40:59.022 [DEBUG] Db connection setup. Database host 10.26.228.71, type PGSQL, name oss, login oss, odbc driver None
2019-11-06 15:40:59.045 [DEBUG] Using database_name: oss(1)
2019-11-06 15:40:59.063 [DEBUG] SQL: set standard_conforming_strings = off (())
2019-11-06 15:40:59.064 [DEBUG] SQL: SELECT 1 (())
2019-11-06 15:40:59.064 [DEBUG] SQL: SELECT 1 FROM pg_class WHERE relname = %s (('openapi_config',))
2019-11-06 15:40:59.065 [DEBUG] SQL: SELECT require_auth, use_ssl FROM openapi_config (())
2019-11-06 15:40:59.066 [DEBUG] OpenAPI init, settings: {'host': '10.26.228.71', 'user': None, 'https': False, 'initialized': True, 'password': '***', 'timeout': None, 'port': '8440'}
...
...
2019-11-06 15:41:02.326 [INFO] addBrandRewriteRules finished successfully

If you understand that you have added wrong rules by mistake, you can remove them by specify parameter "remove" in script. But in this case task "Update configuration for brand" will not be triggered and it's gonna have to be run manually. Example:


$ python custom_rewrite_rules.py remove 4 rules.txt
2019-11-06 15:53:53.037 [DEBUG] read agent config from /usr/local/pem/etc/pleskd.props
2019-11-06 15:53:53.037 [DEBUG] read kernel config from /usr/local/pem/etc/Kernel.conf
2019-11-06 15:53:53.037 [DEBUG] read odbc config from /usr/local/pem/etc/odbc.ini
2019-11-06 15:53:53.038 [DEBUG] read agent config from /usr/local/pem/etc/pleskd.props
2019-11-06 15:53:53.038 [DEBUG] read kernel config from /usr/local/pem/etc/Kernel.conf
2019-11-06 15:53:53.039 [DEBUG] read odbc config from /usr/local/pem/etc/odbc.ini
2019-11-06 15:53:53.039 [DEBUG] Db connection setup. Database host 10.26.228.71, type PGSQL, name oss, login oss, odbc driver None
2019-11-06 15:53:53.062 [DEBUG] Using database_name: oss(1)
2019-11-06 15:53:53.082 [DEBUG] SQL: set standard_conforming_strings = off (())
2019-11-06 15:53:53.082 [DEBUG] SQL: SELECT 1 (())
2019-11-06 15:53:53.083 [DEBUG] SQL: SELECT 1 FROM pg_class WHERE relname = %s (('openapi_config',))
2019-11-06 15:53:53.085 [DEBUG] SQL: SELECT require_auth, use_ssl FROM openapi_config (())
2019-11-06 15:53:53.086 [DEBUG] OpenAPI init, settings: {'host': '10.26.228.71', 'user': None, 'https': False, 'initialized': True, 'password': '***', 'timeout': None, 'port': '8440'}
2019-11-06 15:53:53.113 [DEBUG] call method pem.removeBrandRewriteRules({'brand_id': 4, 'referer': {'domain': 'custom_rewrite_rule_domain', 'kind': 'custom_rewrite_rule_kind', 'id': -9999}, 'txn_id': '1001922472'})
2019-11-06 15:53:53.163 [DEBUG] return {'status': 0}
2019-11-06 15:53:53.210 [DEBUG] call method pem.getRequestStatus({'request_id': '1573044833.087165req'})
2019-11-06 15:53:53.228 [DEBUG] return {'status': 0, 'result': {'request_status': 0, 'status_messages': []}}
2019-11-06 15:53:53.228 [INFO] removeBrandRewriteRules finished successfully

Internal content

Link on internal Article