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/publicwith 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.
Step 2: Add HTTPS Support (Recommended)
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_URLbased on the request domain:
Register this middleware in// 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); }app/Http/Kernel.phpto apply it to all requests.
Final Steps
- Enable the new virtual host files with
sudo a2ensite client1.com.conf(repeat for each client domain). - Restart Apache again to apply all changes:
sudo systemctl restart apache2. - Test by visiting
client1.comin your browser—you’ll see the content fromexample.com/client1.comwithout any URL redirect.
内容的提问来源于stack exchange,提问作者Abhishek
相关产品推荐
相关产品推荐

