如何在.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.phpor 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 toR=302temporarily to avoid browser caching issues.
How to Test:
- Use curl to verify the excluded page doesn't redirect:
You should see acurl -I http://www.yourdomain.com/sms-delivery-report200 OKstatus, not a301 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
相关产品推荐
相关产品推荐

