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

本地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:

  1. Download & extract Nginx: Grab the Windows version from the official Nginx site, then unzip it to a folder like C:\nginx.
  2. 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:
    openssl req -x509 -newkey rsa:4096 -nodes -keyout domain1.key -out domain1.crt -days 365 -subj "/CN=domain1.com"
    
    Create a cert folder inside C:\nginx and move the domain1.key and domain1.crt files there.
  3. Edit Nginx config: Open C:\nginx\conf\nginx.conf and add this server block inside the http section:
    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;
        }
    }
    
  4. Update hosts file: Open C:\Windows\System32\drivers\etc\hosts as Administrator and add:
    127.0.0.1 domain1.com
    
  5. Start Nginx: Open Command Prompt, navigate to C:\nginx, and run start nginx. Test by visiting https://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:

  1. 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
    
  2. Configure virtual hosts: Open conf/extra/httpd-vhosts.conf and 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>
    
  3. Add SSL cert: Generate the same self-signed cert as in the Nginx step, then move the .crt to C:/xampp/apache/conf/ssl.crt and .key to C:/xampp/apache/conf/ssl.key.
  4. Update hosts file: Same as the Nginx step—add 127.0.0.1 domain1.com.
  5. 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:

  1. Install Nginx:
    • For Debian/Ubuntu: sudo apt update && sudo apt install nginx
    • For RHEL/CentOS: sudo dnf install nginx
  2. 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"
    
  3. Create Nginx config file: Make a new file at /etc/nginx/sites-available/domain1.conf with 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;
        }
    }
    
  4. Enable the config: Create a symlink to enable the site:
    sudo ln -s /etc/nginx/sites-available/domain1.conf /etc/nginx/sites-enabled/
    
  5. Update hosts file: Add the domain mapping:
    echo "127.0.0.1 domain1.com" | sudo tee -a /etc/hosts
    
  6. Test & restart Nginx:
    sudo nginx -t  # Checks for config errors
    sudo systemctl restart nginx
    
    Visit https://domain1.com/test/test.php to verify the redirect.

Option 2: Use Apache

Apache is another solid choice for Linux:

  1. 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
  2. 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>
    
  3. Add SSL cert: Use the same self-signed cert command from the Nginx Linux step.
  4. Update hosts file: Same as Nginx—add 127.0.0.1 domain1.com.
  5. Enable site & restart Apache:
    sudo a2ensite domain1.conf
    sudo systemctl restart apache2
    
    Test your redirect now—it should work as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:04:47