转换HTTP到HTTPS的对齐问题及通过.htaccess实现跳转需求
Fixing HTTPS Upgrade & Non-WWW to WWW Redirects in .htaccess
Hey there! I see you're trying to set up two critical redirect flows: forcing all HTTP traffic to HTTPS, and sending non-WWW requests to the WWW version of your domain. Let's sort out your .htaccess rules to make this work smoothly.
What's Off with Your Current Code
Your existing rule only handles non-WWW to WWW redirects over HTTP, but it doesn't enforce HTTPS for incoming requests, nor does it account for already-HTTPS non-WWW traffic. That's why you're seeing misalignment in your redirect chain.
Corrected .htaccess Rules
Here's the refined code that handles both redirects in the proper order, using permanent 301 redirects (best for SEO and user experience):
RewriteEngine On # 1. First, force all HTTP requests to upgrade to HTTPS RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] # 2. Then, redirect non-WWW HTTPS traffic to WWW HTTPS RewriteCond %{HTTP_HOST} ^assignmentbox\.co.uk$ [NC] RewriteRule ^(.*)$ https://www.assignmentbox.co.uk/$1 [R=301,L] # BEGIN EXPIRES <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 10 days" # Fill in your remaining ExpiresByType rules here (examples below) ExpiresByType text/css "access plus 1 month" ExpiresByType image/jpeg "access plus 6 months" ExpiresByType application/javascript "access plus 1 month" </IfModule> # END EXPIRES
Breakdown of Each Rule
- First Rule: This checks if the request is using HTTP (
%{HTTPS} off) and redirects it to the exact same path over HTTPS. This ensures every user starts on a secure connection first. - Second Rule: Once we're on HTTPS, this checks if the domain is the non-WWW variant (
assignmentbox.co.uk) and sends it to the WWW version. The[NC]flag makes the domain check case-insensitive. - Expires Module: I added a few common
ExpiresByTypeexamples since your original code was cut off—be sure to add all content types relevant to your site to maximize browser caching benefits.
Quick Tips
- Test these redirects in incognito mode or using
curl -Iin your terminal to avoid cached old redirects messing with your testing. - Ensure your SSL certificate covers both
assignmentbox.co.ukandwww.assignmentbox.co.ukto avoid security warnings for users. - 301 redirects are permanent, so double-check the rules before deploying—browsers will cache them for a while!
内容的提问来源于stack exchange,提问作者Kenna
相关产品推荐
相关产品推荐

