简体   繁体   English

当 URL 包含“/ /”时,自定义错误页面未加载

[英]Custom Error page isn't loading when URL contains "/ /"

I'm having a very specific issue with a site I'm building that is set up to use friendly URLs and custom error pages.我正在构建的网站存在一个非常具体的问题,该网站设置为使用友好的 URL 和自定义错误页面。 I've been reading the message boards for days and although there are a lot of similar seeming posts, nothing addresses this problem specifically... at least to my newbie eyes.几天来我一直在阅读留言板,虽然有很多类似的帖子,但没有什么专门解决这个问题......至少在我的新手眼中。

Mostly everything on my site is running ok:我网站上的大部分内容都运行正常:

  • URLs like "example.com/main/variable/variable" work just fine (redirects to example.com/index.php, loads main.php internally, and pulls the variables into an array) “example.com/main/variable/variable”之类的 URL 可以正常工作(重定向到 example.com/index.php,在内部加载 main.php,并将变量拉入数组中)
  • 404 errors are handled by index.php and work fine 404 错误由 index.php 处理并且工作正常
  • 403 errors when you try to access a subfolder with no index page mostly work fine too: "example.com/folder" shows the custom 403 error page at 403.php当您尝试访问没有索引页面的子文件夹时出现 403 错误,大多数情况下也可以正常工作:“example.com/folder”在 403.php 处显示自定义 403 错误页面

Where I'm running into trouble is if the URL gets corrupted to contain a single space followed by a forward slash:我遇到麻烦的地方是 URL 是否被损坏以包含一个空格,后跟一个正斜杠:

  • "example.com/ /" or "example.com/folder/ /" “example.com/ /”或“example.com/folder/ /”
  • "example.com/%20/" or "example.com/folder/%20/" “example.com/%20/”或“example.com/folder/%20/”

When this happens a 403 error gets triggered but it doesn't load the custom 403.php page.发生这种情况时会触发 403 错误,但不会加载自定义 403.php 页面。

Noteably, this problem does not occur if the URL lacks the trailing slash:值得注意的是,如果 URL 缺少尾部斜杠,则不会出现此问题:

  • "example.com/ " or "example.com/%20" “example.com/”或“example.com/%20”
  • "example.com/folder/ " or "example.com/folder/%20" “example.com/folder/”或“example.com/folder/%20”

These all trigger 404 errors rather than 403, which is what I would like to happen when the trailing slash is included.这些都触发 404 错误而不是 403,这是我希望在包含斜杠时发生的情况。

I realize this is a very specific situation that will only occur if someone mangles a url while they're typing it in, but I would love for it to not happen.我意识到这是一种非常特殊的情况,只有当有人在输入 url 时才会发生这种情况,但我希望它不会发生。 I talked with my web host and they said everything is correct on their end.我与我的 web 主机交谈,他们说一切都是正确的。

Here's the code I'm currently running in .htaccess in my /public-html/ directory:这是我当前在 /public-html/ 目录中的 .htaccess 中运行的代码:

ErrorDocument 403 https://www.example.com/403.php

# Begin EnforceSSL 
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]
</IfModule>
# End EnforceSSL

RewriteEngine On
RewriteBase /

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA,NC]

and here's the code in index.php which handles the friendly URLs:这是 index.php 中处理友好 URL 的代码:

<?php

// Create a URL array containing any passed variables, including requested page
$URL = explode("/", $_SERVER[QUERY_STRING]);

// Load the page
if (trim($URL[0]) == "index"){
    require_once("home.php");
} else if (trim(($URL[0]) == "")){
    require_once("home.php");           
} else if (file_exists(strtolower(trim($URL[0])).".php")) {
    require_once(strtolower(trim($URL[0])).".php");
} else {
    require_once("404.php");
}
?>

Any help is greatly appreciated!任何帮助是极大的赞赏!

it's just a default Apache response: "403 Forbidden Forbidden You don't have permission to access / / on this server."它只是一个默认的 Apache 响应:“403 Forbidden Forbidden 您无权访问此服务器//。”

There is likely a rule (mod_security perhaps) that is defined in the server config that is blocking such requests.在阻止此类请求的服务器配置中可能定义了一条规则(可能是 mod_security)。 The issue here is that the ErrorDocument defined in .htaccess is too late to serve your custom response.这里的问题是.htaccess中定义的ErrorDocument为时已晚,无法为您的自定义响应提供服务。 You would need to define the ErrorDocument earlier in the server config.您需要在服务器配置中更早地定义ErrorDocument

If you are on a shared server then you will need to contact your host to debug this further.如果您在共享服务器上,则需要联系您的主机以进一步调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用中间件后,重定向到 url 显示“此页面不起作用”。 但是当我删除中间件时,会显示页面 - After using middleware the redirection to a url is showing "This page isn't working". But when I remove the middleware the page is displayed 当我使用php将Bootstrap轮播图片附加到我的首页时未加载 - Bootstrap carousel image isn't loading when i attach it to my home page using php 如何设置自定义 url 以在页面上加载图像 - How to set custom url for loading images on page Sitemap.xml并未加载,而是调用了index.php?page_url = sitemap.xml - Sitemap.xml isn't loading instead its calling index.php?page_url=sitemap.xml 该页面不工作。 url.com 目前无法处理此请求。 HTTP 错误 500 - The page isn't working. url.com is currently unable to handle this request. HTTP ERROR 500 错误:在此服务器上找不到请求的URL(在刷新页面时,页面在构建后未加载) - Error : The requested URL was not found on this server(page not loading after build when we refresh the page) 此页面无法正常工作HTTP错误500 - This page isn’t working HTTP ERROR 500 页面未正确重定向,代码错误 - The page isn’t redirecting properly error in codeigniter Codeigniter中的“页面未正确重定向”错误 - “The page isn’t redirecting properly” error in codeigniter 页面未正确重定向Codeigniter错误 - Page isn't redirecting properly Codeigniter error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM