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

请求协助:.htaccess配置HTTP及WWW重定向至HTTPS非WWW域名

Hey there, let's fix that annoying redirect issue where your domain goes missing after trying to force HTTPS and www! The problem with your original rule is that when the %{HTTPS} off condition triggers but the request is already using www, the %1 variable ends up empty—since the first RewriteCond (checking for non-www hosts) didn't match and capture anything. That's why you're getting https://www. without the domain.

Here's how to fix this properly, with two solid options depending on whether you want a hardcoded domain or dynamic matching:

Option 1: Hardcoded for example.com (most secure)

This is the safest approach if your domain is fixed. Add these rules at the very top of your <IfModule mod_rewrite.c> block, right after enabling the rewrite engine:

# Force HTTPS and www.example.com (fixed to avoid domain loss)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Option 2: Dynamic domain match (flexible)

If you need this to work with any domain on your server (and you don't have other subdomains to preserve), split the rules to avoid empty variables:

# Force HTTPS first, then enforce www (dynamic domain)
RewriteEngine On

# Redirect non-HTTPS requests to HTTPS + www
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Redirect non-www HTTPS requests to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Where to place these rules in your existing .htaccess

Insert the chosen rule set right after the opening <IfModule mod_rewrite.c> line, before any other directives like DirectoryIndex or existing rewrite rules. Your modified file will look like this (using Option 1 as an example):

<IfModule mod_rewrite.c>
# REWRITE ENGINE CONFIG
RewriteEngine On

# Force HTTPS and www.example.com (fixed rule to avoid domain loss)
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

#ExpiresActive On
DirectoryIndex under_construction.html index.html index.php
#Header set Cache-Control "max-age=259200, public"
#ExpiresDefault "access plus 3 days"
#Header unset ETag
#FileETag None
# Expires header for static content
#<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf|jgz|js.jgz)$">
#Header set Cache-Control "max-age=31536000, public"
#ExpiresDefault "access plus 11 months"
#</FilesMatch>
# Auth for non-public projects
#AuthName "ZEN 2.2 Login"
#AuthType Basic
#AuthUserFile /Applications/MAMP/htdocs/username/.htpasswd
#AuthUserFile F:/wamp/www/username/.htpasswd
#AuthGroupFile /dev/null
#require valid-user
# REWRITE CORE RULES RULES -- PLEASE DON'T MODIFY --
# THESE ARE THE MAIN REWRITES THAT MAKE THE ENTIRE WORLD SPIN
AddDefaultCharset UTF-8
# IF NO IMAGE FOUND
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule .* resources/static/images/no_image.png [L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1&%{QUERY_STRING} [L,QSA]
RewriteRule ^robots.txt$ resources/static/robots.php [L]
# gZip minified js files
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*)\.js$ $1\.js.jgz [L]
AddType "text/javascript" .js.jgz
AddEncoding gzip .jgz
# gZip minified css files
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*)\.css$ $1\.css.jgz [L]
AddType "text/css" .css.jgz
AddEncoding gzip .jgz
</IfModule>

Why your original rule failed

Your original rule combined two conditions with OR: either the host is non-www, or HTTPS is off. When HTTPS is off but the host is already www, the first condition doesn't match—so %1 (the captured group from the first condition) is empty. That's why the redirect URL became https://www.%{REQUEST_URI} without the domain.

By either hardcoding the target domain or splitting the rules to use %{HTTP_HOST} directly (which always holds the current request domain), we avoid that empty variable issue entirely.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:37:40