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

Laravel多独立域名(非子域名)同服务器无跳转展示页面方案

Solution: Serve Client Domains from Laravel Subpath Without Redirect

First, make sure you have mod_rewrite enabled on your Apache server. If you’re on Debian/Ubuntu, run sudo a2enmod rewrite and restart Apache with sudo systemctl restart apache2 to apply the change.

Step 1: Set Up Virtual Hosts for Client Domains

Create a separate Apache configuration file for each client domain (e.g., client1.com.conf, client2.com.conf). Here’s the template for client1.com:

<VirtualHost *:80>
    ServerName client1.com
    ServerAlias www.client1.com

    # Point to your main Laravel project's public directory
    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted

        # Internal rewrite logic to map client domain requests to the subpath
        RewriteEngine On
        RewriteBase /

        # Rewrite non-existent files/dirs to the client subpath
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /client1.com/$1 [L]

        # Serve existing assets (CSS/JS/images) directly from the client subdirectory
        RewriteCond %{REQUEST_URI} !^/client1.com/
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^(.*)$ /client1.com/$1 [L]
    </Directory>

    # Optional logging for debugging
    ErrorLog ${APACHE_LOG_DIR}/client1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/client1.com-access.log combined
</VirtualHost>

Key Details:

  • Replace /var/www/example.com/public with the actual path to your Laravel project’s public folder.
  • The [L] flag tells Apache to stop processing further rewrite rules after this one, ensuring the internal redirect happens without changing the user’s browser URL.

If you’re using SSL (which you should), create a similar config for port 443 with your SSL certificates:

<VirtualHost *:443>
    ServerName client1.com
    ServerAlias www.client1.com

    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public>
        # Same rewrite rules as the HTTP config
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted

        RewriteEngine On
        RewriteBase /

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /client1.com/$1 [L]

        RewriteCond %{REQUEST_URI} !^/client1.com/
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule ^(.*)$ /client1.com/$1 [L]
    </Directory>

    # SSL certificate paths
    SSLEngine On
    SSLCertificateFile /path/to/your/client1.crt
    SSLCertificateKeyFile /path/to/your/client1.key
    SSLCertificateChainFile /path/to/your/chain.crt # If required by your SSL provider

    ErrorLog ${APACHE_LOG_DIR}/client1.com-ssl-error.log
    CustomLog ${APACHE_LOG_DIR}/client1.com-ssl-access.log combined
</VirtualHost>

Step 3: Laravel Route & URL Adjustments

  • Route Setup: Ensure your Laravel routes are configured to handle the client subpath. For example:
    Route::prefix('client1.com')->group(function () {
        Route::get('/', function () {
            return view('clients.client1.home');
        });
        // Add other client-specific routes here
    });
    
  • Dynamic APP_URL: To generate correct URLs in views (e.g., for assets or links), add a middleware to set the APP_URL based on the request domain:
    // app/Http/Middleware/SetClientAppUrl.php
    public function handle(Request $request, Closure $next)
    {
        $protocol = $request->isSecure() ? 'https://' : 'http://';
        config(['app.url' => $protocol . $request->getHost()]);
        return $next($request);
    }
    
    Register this middleware in app/Http/Kernel.php to apply it to all requests.

Final Steps

  1. Enable the new virtual host files with sudo a2ensite client1.com.conf (repeat for each client domain).
  2. Restart Apache again to apply all changes: sudo systemctl restart apache2.
  3. Test by visiting client1.com in your browser—you’ll see the content from example.com/client1.com without any URL redirect.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:53:02