如何解决XAMPP:错误 404 找不到对象!调用 API 时在此服务器上找不到请求的 URL
我在 Windows 10 中使用 xampp 来运行在登录时调用 API 的 slimframework。
如果我输入 localhost/maps.html,则可以访问该站点,但如果我尝试单击 index.html 中的登录按钮,则不会调用登录 API。
我已经配置了 httpd
和 httpd-vhosts
,该站点可以在本地访问,但是每当我尝试单击 index.html 中的登录按钮时,就会出现此错误消息:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error,please contact the webmaster.
Error 404
localhost
Apache/2.4.43 (Win64) OpenSSL/1.1.1g PHP/7.4.7
这是我的目录结构:
根目录下:
-index.php
-maps.html
-index.html
-admin/
-js/
-vendor/
-.htaccess
在 index.html 中:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Maps</title>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/knockout/knockout-3.0.0.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/login.js"></script>
<script type="text/javascript" src="js/lib/jquery.cookie.js"></script>
<link rel="icon" href="favicon.ico" />
</head>
<body>
<div id="header">
<h1 data-bind="text:site_title"></h1>
</div>
<div id="login_container">
<div id="login_view">
<div>
<input id="loginid" type="text" data-bind="value: loginid,event:{keypress:kpLoginId}" placeholder="LoginID" />
</div>
<div>
<input id="password" type="password" data-bind="value: password,event:{keypress:kpPassword}" placeholder="Password" />
</div>
<div>
<button id="login" data-bind="click: login">Login</button>
</div>
</div>
</div>
<div data-bind="visible: failed">
Cannot Login
</div>
</body>
</html>
在 httpd.conf 内:
DocumentRoot "C:\xampp\htdocs\myslimsite\web_src"
<Directory "C:\xampp\htdocs\myslimsite\web_src">
#
# Possible values for the Options directive are "None","All",# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All","None",or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html index.php index.pl index.cgi index.asp index.shtml index.htm \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>
.htaccess 内部:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/api/v1/(.*)$ /index.php/$1 [QSA,L]
除了 httpd.conf 和 httpd-vhosts.conf 之外,是否还有其他设置需要配置才能使其他页面正常工作?谢谢。
解决方法
重写规则 ^/api/v1/(.*)$ /index.php/$1 [QSA,L]
这不是 Slim 的工作方式。您必须向“前端控制器”添加内部重定向。前端控制器是 index.php 文件。然后在 Slim 中而不是在 .htaccess
文件中定义路由。
请先阅读文档:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。