提问人:Fabrice Quenneville 提问时间:10/25/2023 更新时间:10/25/2023 访问量:17
如何处理apache中的多个错误?/ 我的htaccess有什么问题?
How to handle multiple errors in apache? / What's wrong with my htaccess?
问:
因此,我正在为一个朋友重写一个旧网站,并且我为php网站使用了我常用的htaccess文件,并为移动或删除的URL添加了一些重定向。但是当我测试重定向或删除 URL 以测试错误处理程序时,apache 给了我一些错误,说 404 在 410 期间发生了。
我已经将问题隔离到我的 php 扩展删除器和 301/410 冲突,但我尝试过的一切我仍然会遇到不希望的错误或行为。
所以这是我要做的:
# Documentation: https://httpd.apache.org/docs/2.4/rewrite/flags.html
# Expires
ExpiresActive On
ExpiresDefault "access plus 2 days"
# Images
AddType image/x-icon ico
AddType image/webp webp
ExpiresByType image/* "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/pdf "access plus 1 year"
ExpiresByType text/x-javascript "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
# Fonts
AddType application/vnd.ms-fontobject eot
AddType application/x-font-ttf ttf ttc
AddType font/opentype otf
AddType application/x-font-woff woff
AddType application/x-font-woff2 woff2
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType font/opentype "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType application/x-font-woff2 "access plus 1 year"
# Rewrites
RewriteEngine on
# Erreurs
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 410 /error.php
# Autre rewrites
DirectorySlash Off # Fixes the issue where a page and folder can have the same name. See https://stackoverflow.com/questions/2017748
# Disable auto-generated directory listings (mod_autoindex)
Options -Indexes
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Redirect some/path/fr to some/path/fr/
RewriteRule ^(en|fr)$ %{REQUEST_URI}/ [L,R=301]
# Permanent redirects for old URLs to new URLs
Redirect 301 /oldpath/oldpage.php /fr/newpath/newpage
Redirect 301 /oldpath/oldpageen.php /en/newpath/newpage
# Handle removal of old pages with a 410 status
RewriteRule ^oldpath/banana\.php$ - [G,L]
RewriteRule ^oldpath/apple\.php$ - [G,L]
# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
# Remove virtual language/locale component
RewriteRule ^(en|fr)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|fr)/$ index.php?lang=$1 [L,QSA]
# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
# RewriteRule ^([a-z]{2})/(.*?)/?$ $2/index.php?lang=$1 [L]
# ExpiresByType "access plus 1 month"
但是当去 http://somedom.xyz/oldpath/banana.php
它触发:
RewriteRule ^oldpath/banana\.php$ - [G,L]
和
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
给出此错误:
Gone
The requested resource is no longer available on this server and there is no forwarding address. Please remove all references to this resource.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.4.56 (Debian) Server at
但是,删除这两个规则之一会触发正确的、现已隔离的错误和错误处理页面。
当去 http://somedom.xyz/oldpath/oldpage.php
它触发:
Redirect 301 /oldpath/oldpage.php /fr/newpath/newpage
和
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
给出此错误:
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
但是当去 http://somedom.xyz/oldpath/oldpage 时,它会重定向到 http://somedom.xyz/fr/newpath/newpage。
我尝试了一些方法,包括排除特定的URL:
# Return a 404 status for URLs with .php extension
RewriteCond %{REQUEST_URI} \.php$
# Exclude specific URLs from this rule
RewriteCond %{REQUEST_URI} !^/oldpath/oldpage\.php$
RewriteRule .* - [L,R=404]
我还尝试过它不会在现有错误时触发,但这不起作用:
# Set an environment variable if an error status is set
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^ - [E=NO_ERROR:1]
# Handle 404 for URLs with .php extension if NO_ERROR is not set
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteCond %{ENV:NO_ERROR} !1
RewriteRule .* - [L,R=404]
两者仍然给出:
Gone
The requested resource is no longer available on this server and there is no forwarding address. Please remove all references to this resource.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
我不确定此时该怎么做,但请忽略旧 URL,但我仍然遇到旧 URL 的访问者最终出现双 404(缺少页面 + php 文件扩展名)的问题,从而导致 apache 错误页面而不是我的错误处理页面。
有人有推荐吗?
有没有办法覆盖任何错误,甚至是多个错误,将访问者发送到错误处理程序?
谢谢
答: 暂无答案
评论