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

转换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 ExpiresByType examples 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 -I in your terminal to avoid cached old redirects messing with your testing.
  • Ensure your SSL certificate covers both assignmentbox.co.uk and www.assignmentbox.co.uk to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:27:29