Apache如何将域名映射到同一主机下WordPress站点的特定路径?
Absolutely! You can pull this off with Apache's built-in modules—no separate WordPress install required. Let's walk through exactly how to set this up:
1. Enable Required Apache Modules
First, make sure the necessary modules are active on your server. On Debian/Ubuntu systems, run these commands:
sudo a2enmod proxy proxy_http rewrite substitute sudo systemctl restart apache2
For RHEL/CentOS, use httpd instead of apache2 and enable modules via your httpd.conf or .conf files in /etc/httpd/conf.d/.
2. Configure the New Domain's Virtual Host
Create a virtual host config for myshopdotcom.example (usually stored in /etc/apache2/sites-available/ on Debian/Ubuntu, or /etc/httpd/conf.d/ on RHEL/CentOS). Here's the full working setup:
<VirtualHost *:80> ServerName myshopdotcom.example # Enable rewrite engine RewriteEngine On # Proxy all requests to the matching path on your original WordPress site RewriteRule ^/(.*)$ http://example.com/$1 [P,L] # Fix redirects sent by WordPress (prevents bouncing back to example.com) ProxyPassReverse / http://example.com/ # Update cookies to use the new domain (critical for sessions/logins) ProxyPassReverseCookieDomain example.com myshopdotcom.example ProxyPassReverseCookiePath / / # Replace all example.com references in HTML content with the new domain # Ensures internal links don't send users back to the original URL AddOutputFilterByType SUBSTITUTE text/html Substitute "s|example.com|myshopdotcom.example|ni" # Optional: Set up logging (adjust paths to match your server) ErrorLog ${APACHE_LOG_DIR}/myshopdotcom-error.log CustomLog ${APACHE_LOG_DIR}/myshopdotcom-access.log combined </VirtualHost>
Key Config Breakdown:
RewriteRule ^/(.*)$ http://example.com/$1 [P,L]: The[P]flag tells Apache to proxy the request (not redirect it) — users will seemyshopdotcom.examplein their browser while content loads from your existing WordPress site.ProxyPassReverse: Corrects any redirects generated by WordPress (like login flow redirects) to point to the new domain instead ofexample.com.ProxyPassReverseCookieDomain/ProxyPassReverseCookiePath: Updates cookie metadata to match the new domain, so user sessions and authentication work seamlessly.AddOutputFilterByType+Substitute: Scans HTML responses and swaps outexample.comwithmyshopdotcom.example, fixing internal links that would otherwise lead back to the original domain.
3. Enable the Virtual Host
On Debian/Ubuntu, activate the site and reload Apache:
sudo a2ensite myshopdotcom.example.conf sudo systemctl reload apache2
On RHEL/CentOS, just restart httpd after saving the config file.
4. Set Up DNS
Make sure the DNS record for myshopdotcom.example points to the same server IP as example.com — use an A record or CNAME depending on your DNS provider's setup.
5. HTTPS Adjustments (If Using SSL)
If you want to serve myshopdotcom.example over HTTPS:
- Get an SSL certificate for the new domain (use Let's Encrypt or your certificate provider).
- Update the virtual host to use port 443:
<VirtualHost *:443> ServerName myshopdotcom.example # SSL Configuration (update paths to your cert files) SSLEngine On SSLCertificateFile /path/to/your/cert.pem SSLCertificateKeyFile /path/to/your/privkey.pem SSLCertificateChainFile /path/to/your/chain.pem # Same proxy/substitute rules as above, using HTTPS for the target RewriteEngine On RewriteRule ^/(.*)$ https://example.com/$1 [P,L] ProxyPassReverse / https://example.com/ ProxyPassReverseCookieDomain example.com myshopdotcom.example ProxyPassReverseCookiePath / / AddOutputFilterByType SUBSTITUTE text/html Substitute "s|example.com|myshopdotcom.example|ni" ErrorLog ${APACHE_LOG_DIR}/myshopdotcom-ssl-error.log CustomLog ${APACHE_LOG_DIR}/myshopdotcom-ssl-access.log combined </VirtualHost>
- Optionally, add a redirect from HTTP to HTTPS in the port 80 virtual host for
myshopdotcom.example.
Final Checks
Visit myshopdotcom.example/shop and myshopdotcom.example/products/sample-product — you should see the exact same content as your original WordPress pages, with the new domain staying in the browser address bar. If links still point to example.com, double-check the Substitute rule to ensure it's targeting all HTML content.
内容的提问来源于stack exchange,提问作者Faizan Ali

