PHP 路由不包括文件
PHP routing not including files
我为 PHP 尝试了一个简单的路由,但我越来越抓狂了,因为它不能完全正常工作...
希望任何人都可以提供帮助。这是我第一次尝试使用 PHP 编写自己的路由。
问题
当我请求“SERVER-IP/hms/routing/test”时,它显示 home.php 的内容 - ok
当我请求“SERVER-IP/hms/routing/shop”时,它显示 404 错误.... - 为什么??
一旦我在服务器上删除“shop.php”,它就会显示我的 index.php 和
Request:/hms/routing/shop
URL: /shop
不知道为什么没有正确加载文件...
知道为什么它不起作用吗? :(
我的.htaccess
RewriteEngine On
RewriteBase /hms/routing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [L,QSA]
我的index.php
<?php
$request = $_SERVER['REQUEST_URI'];
echo "Request:".$request."<br>";
$url = str_replace("/hms/routing","", $request);
echo "URL: ".$url."<br>";
switch ($url) {
case "/":
include "home.php";
break;
case "/about":
include "about.php";
break;
case "/test":
include "home.php";
break;
case "/shop":
include "shop.php";
break;
}
?>
提前致谢! :)
终于成功了!
感谢Will B.,解决方法如下:
.htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /hms/routing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [L,QSA]
index.php
<?php
$request = $_SERVER['REQUEST_URI'];
echo "Request:".$request."<br>";
$url = str_replace("/hms/routing","", $request);
$url = trim($url);
echo "URL: ".$url."<br>";
echo "Complete: ". __DIR__ . $url.".php<br>";
switch ($url) {
case "/":
require_once __DIR__ . '/home.php';
break;
case "/about":
require_once __DIR__ . '/about.php';
break;
case "/test":
require_once __DIR__ . '/home.php';
break;
case "/shop":
require_once __DIR__ . '/shop.php';
break;
}
我认为最终有用的是 Options -MultiViews
in .htaccess
非常感谢您的帮助! :)
我为 PHP 尝试了一个简单的路由,但我越来越抓狂了,因为它不能完全正常工作...
希望任何人都可以提供帮助。这是我第一次尝试使用 PHP 编写自己的路由。
问题
当我请求“SERVER-IP/hms/routing/test”时,它显示 home.php 的内容 - ok
当我请求“SERVER-IP/hms/routing/shop”时,它显示 404 错误.... - 为什么??
一旦我在服务器上删除“shop.php”,它就会显示我的 index.php 和
Request:/hms/routing/shop
URL: /shop
不知道为什么没有正确加载文件...
知道为什么它不起作用吗? :(
我的.htaccess
RewriteEngine On
RewriteBase /hms/routing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [L,QSA]
我的index.php
<?php
$request = $_SERVER['REQUEST_URI'];
echo "Request:".$request."<br>";
$url = str_replace("/hms/routing","", $request);
echo "URL: ".$url."<br>";
switch ($url) {
case "/":
include "home.php";
break;
case "/about":
include "about.php";
break;
case "/test":
include "home.php";
break;
case "/shop":
include "shop.php";
break;
}
?>
提前致谢! :)
终于成功了!
感谢Will B.,解决方法如下:
.htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /hms/routing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [L,QSA]
index.php
<?php
$request = $_SERVER['REQUEST_URI'];
echo "Request:".$request."<br>";
$url = str_replace("/hms/routing","", $request);
$url = trim($url);
echo "URL: ".$url."<br>";
echo "Complete: ". __DIR__ . $url.".php<br>";
switch ($url) {
case "/":
require_once __DIR__ . '/home.php';
break;
case "/about":
require_once __DIR__ . '/about.php';
break;
case "/test":
require_once __DIR__ . '/home.php';
break;
case "/shop":
require_once __DIR__ . '/shop.php';
break;
}
我认为最终有用的是 Options -MultiViews
in .htaccess
非常感谢您的帮助! :)