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

如何在.htaccess中强制HTTPS和www但排除指定SMS报告接收页面?

Fixing .htaccess Redirect Exclusion for SMS Delivery Report Page

Got it, let's sort out this .htaccess redirect issue you're dealing with. The core problem here is that your global HTTPS/www redirect is overriding your exclusion rule—since .htaccess processes rules top to bottom, we need to explicitly let the SMS report page bypass those redirects first.

Here's a tested configuration that should work for your scenario:

# Enable rewrite engine
RewriteEngine On

# 1. EXCLUDE the SMS delivery report page from all redirects
# Replace "/sms-delivery-report" with your actual page path (e.g., /api/sms-report.php)
RewriteCond %{REQUEST_URI} ^/sms-delivery-report$ [NC]
RewriteRule ^ - [L]

# 2. Redirect non-www to www (applies to both HTTP and HTTPS)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# 3. Redirect HTTP (www) to HTTPS—except for our excluded page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/sms-delivery-report$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Key Details to Note:

  • Rule Order Matters: The exclusion rule comes first, marked with [L] (last rule) so any request matching the SMS page stops here and isn't processed by the redirect rules below.
  • Exact Path Matching: The ^/sms-delivery-report$ regex ensures we only match the exact page (adjust this if your page has a file extension like .php or a longer path). The [NC] flag makes the match case-insensitive.
  • 301 vs 302: I used R=301 (permanent redirect) since your global redirect is permanent, but if you're testing, switch to R=302 temporarily to avoid browser caching issues.

How to Test:

  • Use curl to verify the excluded page doesn't redirect:
    curl -I http://www.yourdomain.com/sms-delivery-report
    
    You should see a 200 OK status, not a 301 Moved Permanently.
  • Clear your browser cache before testing, as 301 redirects are cached aggressively.

Common Pitfalls to Avoid:

  • Forgetting the [L] flag on the exclusion rule: This lets subsequent redirect rules still apply to the page, which is why your previous attempts failed.
  • Incorrect regex matching: If your page path has a trailing slash or query parameters, adjust the regex (e.g., ^/sms-delivery-report/?$ to allow an optional trailing slash).
  • Mixing up rule order: Always put exclusion rules before global redirects.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:10:17