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

.htaccess中匹配域名并实现指定规则的请求重定向方法

Fixing Your Redirect Rules (No More Test Subdomain Hijacks!)

Got it, let's get those redirects working exactly how you need them. The issue with your current setup is probably that your rules aren't tightly targeting the specific hostnames, so all requests are getting caught by the test subdomain rule by mistake. Let's break this down for both Apache (mod_rewrite) and Nginx, since those are the most common servers.

Apache (mod_rewrite) Setup

First, make sure mod_rewrite is enabled on your server. Then add these rules to your .htaccess file or Apache config:

# Enable rewrite engine
RewriteEngine On

# Rule 1: Redirect HTTPS test subdomain to HTTP, preserve full URI
RewriteCond %{HTTP_HOST} ^test\.canvas-events\.co\.uk$
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

# Rule 2: Redirect non-HTTPS main domain (with or without www) to HTTPS www
RewriteCond %{HTTP_HOST} ^(www\.)?canvas-events\.co\.uk$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.canvas-events.co.uk/$1 [R=301,L]

What Each Rule Does:

  • Rule 1: First checks if the request is for test.canvas-events.co.uk AND uses HTTPS. If both are true, it redirects to the HTTP version, keeping the full URI (the $1 captures everything after the domain). The L flag ensures no other rules run after this one for matching requests.
  • Rule 2: Checks if the request is for either canvas-events.co.uk or www.canvas-events.co.uk AND uses HTTP. It then redirects to the HTTPS www version, again preserving the full URI.

Nginx Setup

If you're using Nginx, add these server blocks to your config:

# Rule 1: Handle HTTPS test subdomain redirect to HTTP
server {
    listen 443 ssl;
    server_name test.canvas-events.co.uk;

    # Add your SSL cert config here (certificate, key, etc.)

    return 301 http://test.canvas-events.co.uk$request_uri;
}

# Rule 2: Handle non-HTTPS main domain redirect to HTTPS www
server {
    listen 80;
    server_name canvas-events.co.uk www.canvas-events.co.uk;

    return 301 https://www.canvas-events.co.uk$request_uri;
}

# Don't forget your main HTTPS server block for www.canvas-events.co.uk
server {
    listen 443 ssl;
    server_name www.canvas-events.co.uk;

    # Your SSL cert config and site content here
}

Critical Notes:

  • Use 301 redirects for permanent changes (like domain/HTTPS migrations) but switch to 302 temporarily while testing—browser caching can make old redirects stick around and skew your results.
  • After updating your config, clear your browser cache or test in incognito mode to avoid cached old redirects interfering.
  • Double-check that your server is configured to recognize all three hostnames (test subdomain, bare main domain, www main domain).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:32:16