页面 404 不适用于 .htaccess?
The Page 404 not working with .htaccess?
我的 .htaccess 文件中有此代码:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz= [L,QSA]
ErrorDocument 404 https://example.com/erorrpage.php
ErrorDocument 403 https://example.com/erorrpage.php
我有一个这样的论点:
我假设您使用的是 apache。
- 确保您的 .htaccess 文件位于正确的目录(例如根目录)或
erorrpage.php
可在 https://example.com/
上访问
- 尝试用
ErrorDocument 404 "Custom very simple 404 error"
简化它,看看它是否有效
- 确保您的 mod 重写已启用(在
httpd.conf
上,如果未启用,请取消注释 ;LoadModule rewrite_module modules/mod_rewrite.so
。然后重新启动 apache)
- 找到您的 www 根目录的相关目录标签(换句话说,在您的根目录的 apache 配置中)并将
AllowOverride None
更改为 AllowOverride All
- 如果问题发生在虚拟主机中,请确保它允许 .htaccess
- 如果所有这些都不起作用,请尝试检查 apache official docs
您的 ErrorDocument
没有被触发,因为您在 htaccess
中有以下规则
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz= [L,QSA]
该规则将目录 !-d
和 !-f
文件中不存在的所有内容重定向到 /index.php
。由于 mod-rewrite
规则在 mod-core
之前应用,您的 ErrorDocument 将永远不会被应用。
要解决这个问题,您需要使用 mod-rewrite
规则而不是 ErrorDocument 将 non-existent 请求重定向到 404 页面。所以将以下内容放在 htaccess
的顶部
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ - [R=404,L]
如果你想将 404 请求重定向到特定页面,你可以使用以下
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /errorpage.php [R= 404,L]
我的 .htaccess 文件中有此代码:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz= [L,QSA]
ErrorDocument 404 https://example.com/erorrpage.php
ErrorDocument 403 https://example.com/erorrpage.php
我有一个这样的论点:
我假设您使用的是 apache。
- 确保您的 .htaccess 文件位于正确的目录(例如根目录)或
erorrpage.php
可在https://example.com/
上访问
- 尝试用
ErrorDocument 404 "Custom very simple 404 error"
简化它,看看它是否有效 - 确保您的 mod 重写已启用(在
httpd.conf
上,如果未启用,请取消注释;LoadModule rewrite_module modules/mod_rewrite.so
。然后重新启动 apache) - 找到您的 www 根目录的相关目录标签(换句话说,在您的根目录的 apache 配置中)并将
AllowOverride None
更改为AllowOverride All
- 如果问题发生在虚拟主机中,请确保它允许 .htaccess
- 如果所有这些都不起作用,请尝试检查 apache official docs
您的 ErrorDocument
没有被触发,因为您在 htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz= [L,QSA]
该规则将目录 !-d
和 !-f
文件中不存在的所有内容重定向到 /index.php
。由于 mod-rewrite
规则在 mod-core
之前应用,您的 ErrorDocument 将永远不会被应用。
要解决这个问题,您需要使用 mod-rewrite
规则而不是 ErrorDocument 将 non-existent 请求重定向到 404 页面。所以将以下内容放在 htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ - [R=404,L]
如果你想将 404 请求重定向到特定页面,你可以使用以下
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /errorpage.php [R= 404,L]