首页 > 解决方案 > htaccess 使用 301 重定向强制 https 但发现它实际上是 302

问题描述

[更新]

我激活了始终使用 HTTPS,现在它重定向两次到 301(第一次是 https,第二次是 http)然后到 200(https),但仍然不知道为什么会发生这种重定向

在此处输入图像描述


当我使用https://www.redirectcheck.com检查我的网站时遇到问题,它显示重定向到 301,然后到 302,然后状态为 200。当此页面不在公共缓存控制中但当它是公开时,就会发生这种情况缓存它直接进入状态 200。这是一个连接到 cloudflare 的 Laravel 项目,它位于 root .htaccess 中

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*) https://example.com/$1  [QSA,L,R=301]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

#remove 'public/' from the url
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php [L]
</IfModule>

而这个 .htaccess 在公用文件夹中

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
</IfModule>

和路由中间件

Route::middleware('cache.headers:public')->group(function() {

Cloudflare:

在此处输入图像描述

在此处输入图像描述

为什么它说重定向是 302 而不是 301,这将如何影响 SEO?

example.com

HTTP/1.1 302 Found
Date: Mon, 26 Oct 2020 20:37:43 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: xxxx; expires=Wed, 25-Nov-20 20:37:43 GMT; path=/; 
domain=.example.com; HttpOnly; SameSite=Lax
X-Powered-By: PHP/7.4.6
Cache-Control: no-cache, private
Location: https://example.com
CF-Cache-Status: DYNAMIC
cf-request-id: xxxx
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=xxxx"}],"group":"cf-nel","max_age":604800}
NEL: {"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: xxxx

https://example.com

HTTP/2 200 
date: Mon, 26 Oct 2020 20:37:45 GMT
content-type: text/html; charset=UTF-8
set-cookie: xxxx; expires=Wed, 25-Nov-20 20:37:43 GMT; path=/; 
domain=.example.com; HttpOnly; SameSite=Lax
x-powered-by: PHP/7.4.6
cache-control: public
set-cookie: hide_cookie_bar=1; expires=Wed, 25-Nov-2020 20:37:44 GMT; Max- 
Age=2592000; path=/
set-cookie: XSRF-TOKEN=xxxx; expires=Mon, 26-Oct-2020 22:37:44 GMT; Max- 
Age=7200; path=/
set-cookie: laravel_session=xxxx; expires=Mon, 26-Oct-2020 22:37:44 GMT; Max- 
Age=7200; path=/; httponly
vary: Accept-Encoding
cf-cache-status: DYNAMIC
cf-request-id: xxxx
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=xxxx"}],"group":"cf-nel","max_age":604800}
nel: {"report_to":"cf-nel","max_age":604800}
server: cloudflare
cf-ray: xxxx

标签: laravel.htaccessredirecthttpscloudflare

解决方案


推荐阅读