本地Windows/Linux环境下将https://domain1.com重定向至https://domain2.com的方法
Solution for Path-Specific Redirect Between Domains (Windows & Linux)
Alright, let's clear this up first: the hosts file only maps domain names to IP addresses—it can't handle path-specific redirects like you're trying to do. That's why your initial attempt didn't work. Below are step-by-step solutions for both Windows and Linux environments using common web servers (Nginx or Apache), which are perfect for this kind of URL routing.
Windows Environment
Option 1: Use Nginx
Nginx is lightweight and easy to configure for redirects. Here's how to set it up:
- Download & extract Nginx: Grab the Windows version from the official Nginx site, then unzip it to a folder like
C:\nginx. - Generate a self-signed SSL certificate: Since you're dealing with HTTPS, you'll need a cert (your browser will show a warning, but it's safe for local use). Use OpenSSL (install via Git Bash or the official OpenSSL installer) and run this command:
Create aopenssl req -x509 -newkey rsa:4096 -nodes -keyout domain1.key -out domain1.crt -days 365 -subj "/CN=domain1.com"certfolder insideC:\nginxand move thedomain1.keyanddomain1.crtfiles there. - Edit Nginx config: Open
C:\nginx\conf\nginx.confand add this server block inside thehttpsection:server { listen 80; listen 443 ssl; server_name domain1.com; ssl_certificate cert/domain1.crt; ssl_certificate_key cert/domain1.key; # Redirect the specific path location /test/test.php { return 301 https://domain2.com$request_uri; } # Proxy all other requests to the original domain1.com location / { proxy_pass https://domain1.com; proxy_set_header Host $host; } } - Update hosts file: Open
C:\Windows\System32\drivers\etc\hostsas Administrator and add:127.0.0.1 domain1.com - Start Nginx: Open Command Prompt, navigate to
C:\nginx, and runstart nginx. Test by visitinghttps://domain1.com/test/test.php—it should redirect to the domain2 version.
Option 2: Use Apache (via XAMPP)
If you already use XAMPP, this is a straightforward choice:
- Enable required modules: Open XAMPP's Apache config (
httpd.conf) and uncomment these lines by removing the#:#LoadModule rewrite_module modules/mod_rewrite.so #LoadModule proxy_module modules/mod_proxy.so #LoadModule proxy_http_module modules/mod_proxy_http.so #LoadModule ssl_module modules/mod_ssl.so #Include conf/extra/httpd-vhosts.conf - Configure virtual hosts: Open
conf/extra/httpd-vhosts.confand add these blocks:<VirtualHost *:80> ServerName domain1.com # Redirect the specific path RedirectMatch 301 ^/test/test.php$ https://domain2.com/test/test.php # Proxy other requests to original domain ProxyPass / https://domain1.com/ ProxyPassReverse / https://domain1.com/ </VirtualHost> <VirtualHost *:443> ServerName domain1.com SSLEngine on SSLCertificateFile "C:/xampp/apache/conf/ssl.crt/domain1.crt" SSLCertificateKeyFile "C:/xampp/apache/conf/ssl.key/domain1.key" RedirectMatch 301 ^/test/test.php$ https://domain2.com/test/test.php ProxyPass / https://domain1.com/ ProxyPassReverse / https://domain1.com/ </VirtualHost> - Add SSL cert: Generate the same self-signed cert as in the Nginx step, then move the
.crttoC:/xampp/apache/conf/ssl.crtand.keytoC:/xampp/apache/conf/ssl.key. - Update hosts file: Same as the Nginx step—add
127.0.0.1 domain1.com. - Restart Apache: Use the XAMPP control panel to stop and start Apache. Test your redirect.
Linux Environment
Option 1: Use Nginx
Most Linux distros make installing Nginx a breeze:
- Install Nginx:
- For Debian/Ubuntu:
sudo apt update && sudo apt install nginx - For RHEL/CentOS:
sudo dnf install nginx
- For Debian/Ubuntu:
- Generate SSL cert: Run this command to create a self-signed cert (stored in system default SSL folders):
sudo openssl req -x509 -newkey rsa:4096 -nodes -keyout /etc/ssl/private/domain1.key -out /etc/ssl/certs/domain1.crt -days 365 -subj "/CN=domain1.com" - Create Nginx config file: Make a new file at
/etc/nginx/sites-available/domain1.confwith this content:server { listen 80; listen 443 ssl; server_name domain1.com; ssl_certificate /etc/ssl/certs/domain1.crt; ssl_certificate_key /etc/ssl/private/domain1.key; location /test/test.php { return 301 https://domain2.com$request_uri; } location / { proxy_pass https://domain1.com; proxy_set_header Host $host; } } - Enable the config: Create a symlink to enable the site:
sudo ln -s /etc/nginx/sites-available/domain1.conf /etc/nginx/sites-enabled/ - Update hosts file: Add the domain mapping:
echo "127.0.0.1 domain1.com" | sudo tee -a /etc/hosts - Test & restart Nginx:
Visitsudo nginx -t # Checks for config errors sudo systemctl restart nginxhttps://domain1.com/test/test.phpto verify the redirect.
Option 2: Use Apache
Apache is another solid choice for Linux:
- Install Apache & required modules:
- Debian/Ubuntu:
sudo apt update && sudo apt install apache2 libapache2-mod-proxy-html libxml2-dev - Enable modules:
sudo a2enmod rewrite proxy proxy_http ssl
- Debian/Ubuntu:
- Create virtual host config: Make a file at
/etc/apache2/sites-available/domain1.conf:<VirtualHost *:80> ServerName domain1.com RedirectMatch 301 ^/test/test.php$ https://domain2.com/test/test.php ProxyPass / https://domain1.com/ ProxyPassReverse / https://domain1.com/ </VirtualHost> <VirtualHost *:443> ServerName domain1.com SSLEngine on SSLCertificateFile /etc/ssl/certs/domain1.crt SSLCertificateKeyFile /etc/ssl/private/domain1.key RedirectMatch 301 ^/test/test.php$ https://domain2.com/test/test.php ProxyPass / https://domain1.com/ ProxyPassReverse / https://domain1.com/ </VirtualHost> - Add SSL cert: Use the same self-signed cert command from the Nginx Linux step.
- Update hosts file: Same as Nginx—add
127.0.0.1 domain1.com. - Enable site & restart Apache:
Test your redirect now—it should work as expected.sudo a2ensite domain1.conf sudo systemctl restart apache2
内容的提问来源于stack exchange,提问作者Putperest
相关产品推荐
相关产品推荐

