Htaccess 需求:http 到 https - 不存在的文件、文件夹、子域到 404 - 只有第一个是工作


Htaccess Need: http to https - nonexisting files, folders, subdomains to 404 - Only first is working

我需要按照标题中的说明在.htaccess中添加重写,但只有第一个有效,这是我的.htaccess现在的样子:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php [R,L]
ErrorDocument 404 /404.php

不起作用的示例

这两个不存在的子域示例只是挂起然后显示"无法连接",但我希望它们显示 REAL 404 错误页面:

http://nonexisting.example.com
https://nonexisting.example.com

如果我们输入不存在的文件或文件夹,例如:

https://example.com/nonexistingfile.php
https://example.com/nonexistingfolder/

它显示我的 404.php 页面很好,但它们显示为软 404,我希望它显示真正的 404 错误页面。

我已经尝试了大约 30 种不同的第二重写部分变体,但没有成功。一些代码返回了一个真正的 404 代码,但显示了一个混乱的页面,而不是我的 404.php 页面。

这里不需要重写规则,它只是将所有不存在的文件或目录重定向到/404.php R=302 .

只需尝试以下代码:

# will send correct 404 status as well
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,NE,L]