You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter多子域名前端单应用部署方案求助

Single Codebase for Country-Specific Subdomains: A Practical Guide

Great question—maintaining separate codebases for each subdomain is a nightmare for updates, so let’s fix that with a single codebase approach that scales. Here’s how to implement it step by step:

1. Detect the Subdomain at Runtime

The core of this approach is identifying which country subdomain is making a request, then using that context throughout your app.

For example, in a Node.js/Express app, you can extract the country code from the request host:

app.use((req, res, next) => {
  const hostParts = req.headers.host.split('.');
  // Skip non-country subdomains like "www" or "admin"
  if (hostParts[0] !== 'www' && hostParts[0] !== 'admin') {
    req.countryCode = hostParts[0].toLowerCase(); // e.g., "usa", "ca"
  }
  next();
});

In frontend frameworks like React/Vue, parse window.location.hostname on app load to grab the country code, then store it in a context or state manager for global access.

2. Centralize Country-Specific Configuration

Create a single config file to hold all country-specific settings—this keeps your logic clean and makes updates straightforward:

// country-configs.js
export const countryConfigs = {
  usa: {
    currency: 'USD',
    timezone: 'America/New_York',
    supportedPayments: ['credit_card', 'paypal'],
    locale: 'en-US'
  },
  ca: {
    currency: 'CAD',
    timezone: 'America/Toronto',
    supportedPayments: ['credit_card', 'interac'],
    locale: 'en-CA'
  },
  in: {
    currency: 'INR',
    timezone: 'Asia/Kolkata',
    supportedPayments: ['upi', 'credit_card'],
    locale: 'en-IN'
  },
  // Add other countries here
};

Fetch the relevant config using the detected country code wherever you need region-specific behavior.

3. Dynamic Content & Localization

Use internationalization (i18n) libraries to handle language and region-tailored content. For example, with react-i18next:

import i18n from 'i18next';
import { countryConfigs } from './country-configs';

// Initialize i18n with the country's locale
i18n.init({
  lng: countryConfigs[countryCode].locale,
  resources: {
    'en-US': { translation: require('./locales/en-US.json') },
    'en-CA': { translation: require('./locales/en-CA.json') },
    'en-IN': { translation: require('./locales/en-IN.json') },
    // Add other locales
  }
});

You can also dynamically render components (like region-specific payment options) by checking the country config.

4. Server/Proxy Configuration

Make sure all country subdomains route to your single codebase. Using Nginx as an example:

server {
  listen 80;
  server_name *.app-name.com;

  // Route admin subdomain to your backend panel
  if ($host ~* ^admin\.app-name\.com$) {
    proxy_pass http://your-admin-backend:3001;
    break;
  }

  // Forward all other subdomains to your frontend app
  proxy_pass http://your-frontend-app:3000;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
}

Cloud providers like AWS (Route 53 + ALB) or Cloudflare also support wildcard DNS records to route all subdomain traffic to your single application instance.

5. Database: Shared with Contextual Filtering

Since you’re using one database, add a country_code field to relevant tables (users, orders, products). When querying data, automatically filter by the detected country code to ensure users only see region-specific content:

-- Example: Fetch products for the current country
SELECT * FROM products WHERE country_code = ?;

Wrap database queries in a utility layer that includes this filter by default—this avoids repetitive code and prevents cross-region data leaks. For user registration, auto-set the country_code based on the subdomain they signed up from.

6. Local Testing Tips

To test different subdomains locally, edit your hosts file (Linux/macOS: /etc/hosts; Windows: C:\Windows\System32\drivers\etc\hosts) to map subdomains to 127.0.0.1:

127.0.0.1 usa.localhost
127.0.0.1 ca.localhost

Then access http://usa.localhost:3000 to test the US subdomain locally.

This approach keeps your codebase unified, eliminates duplicate maintenance work, and still lets you deliver tailored experiences for each country.

内容的提问来源于stack exchange,提问作者Waqas Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:24:04