Matching the First Subdomain in Odoo Using dbfilter
Introduction
In Odoo, multiple databases can run on the same server to serve different customers or branches using the multi-tenancy system. One common method to select the appropriate database is by using the subdomain (Subdomain) to automatically determine the database.
In this article, we will explain how to configure dbfilter to select the appropriate database based on the subdomain, with practical examples to illustrate the process.
How Does %d Work in dbfilter?
In Odoo, the variable %d represents the first part of the subdomain. For example:
- If the accessed domain is customer1.yourcompany.com, then %d = customer1
- If the domain is sales.team.yourcompany.com, then %d = sales
When using dbfilter = ^%d$, Odoo will look for a database with the same name as the first subdomain.
Configuring dbfilter in odoo.conf
The dbfilter is configured in the odoo.conf file as follows:
This setting ensures that Odoo will only attempt to access a database that matches the first part of the subdomain. Additionally, no-database-list = True prevents listing available databases if no match is found, enhancing security.
Practical Implementation Steps
1. Creating Databases
Databases should be created with names that match the intended subdomains.
Example:
- Database named customer1
- Database named customer2
- Database named sales
2. Configuring DNS
The DNS should be set up to direct subdomains to the Odoo server.
Example:
- customer1.yourcompany.com → Points to the Odoo server IP.
- customer2.yourcompany.com → Same IP.
3. Restarting Odoo
After modifying the odoo.conf file, restart Odoo to apply the changes:
Practical Examples
Example 1: Valid Request with an Existing Database
- Accessed domain: customer1.yourcompany.com
- Existing databases: customer1, customer2, sales
- Result: Odoo automatically directs the user to the customer1 database.
Example 2: Request with a Non-Existing Subdomain
- Accessed domain: unknown.yourcompany.com
- Existing databases: customer1, customer2
- Result:
- If --no-database-list is disabled, the database selection screen will be displayed (a security issue!).
- If --no-database-list is enabled, an "Database not found" error will appear.
Example 3: Domain with www as the First Part
- Accessed domain: www.yourcompany.com
- Existing databases: yourcompany, customer1
- Result: Odoo will attempt to use a database named www, which may not exist.
- Solution:
- Avoid using www as a subdomain.
- Or use a regex pattern to ignore www:
Advanced ConfigurationsAdvanced Configurations
1. Merging the Subdomain with a Fixed Name
If you want all databases to start with client_, you can configure dbfilter as follows:
Example:
- customer1.yourcompany.com → Database: client_customer1
2. Handling Multi-Part Domains
If you have a domain like eu.customer1.yourcompany.com and you want to use a database named customer1_eu, then dbfilter = ^%d_%h$ will not work directly.
The best solution is to use a custom module to dynamically link subdomains to databases.
Important Security Tips
- Always use --no-database-list
To prevent database listing when a match fails:
- Ensure Unique Database Names
Avoid ambiguous names to prevent data conflicts.
- Always Test Before Deployment
Use test domains (e.g., test.yourcompany.com) and test databases.
Conclusion
Using dbfilter in Odoo is an effective way to map databases to subdomains, making it easier to manage multiple customers or branches. By following the steps outlined above, you can improve security and provide a customized user experience based on the accessed domain.
If you need further customization, you can always develop a custom module to extend dbfilter functionality according to your project needs.