Our current frontend app was accessible by multiple domains:
https://boat.com (or https://www.boat.com)
https://car.com
https://plane.com
...
Each domain, was rendering the same app with different content and theme based on the domain name.
We are now trying to merge everything under one unique domain. So instead of accessing the site using its previous domain:
https://car.com
we want to access it using the following url:
https://common.com/car
On the code, we already took care of changing the logic to deliver the correct content based on this new routing system.
We want to redirect automatically our users to the correct routes when they try to access the app from an old domain. Example:
https://boat.com => redirect => https://common.com/boat
https://car.com => redirect => https://common.com/car
...
but we also want to be able to rewrite more complex url:
https://boat.com/categories/name-1 => redirect => https://common.com/boat/categories/name-1
https://car.com/posts/super-title-2 => redirect => https://common.com/car/posts/super-title-2
Is there a way to achieve it using NGINX dynamically/programmatically without having to write and repeat rules for each domains ?
You can obtain the domain name by using a regular expression server_name.
For example:
server {
listen 80;
listen 443 ssl;
ssl_certificate /path/to/$ssl_server_name.crt;
ssl_certificate_key /path/to/$ssl_server_name.key;
server_name ~^(www\.)?(?<domain>[^.]+)\.com$;
return 301 https://example.com/$domain$request_uri;
}
The problem will be selecting the correct SSL certificate for each of the deprecated domains. Nginx has limited capability for selecting certificates dynamically which involves using the $ssl_server_name variable. This means that you will need a directory containing all possible certificate combinations, even if some of those files are identical.
For example:
boat.com.crt
boat.com.key
www.boat.com.crt
www.boat.com.key
car.com.crt
car.com.key
www.car.com.crt
www.car.com.key
Also, you should define a default server, as you do not want this server block to be your default server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With