php - 如何修复 .htaccess 重写不显示 CSS
问题描述
我正在使用本地站点在我的 Mac 上运行 localhost,但我似乎无法让 .htaccess CSS 工作。我正在使用一个 index.php 页面,然后给它 url 变量来告诉要显示哪个页面(例如index.php?login
)。我在这里面临两个问题:第一个是它重定向到不包含我的全局样式的页面,第二个是它显示主页而不是我的登录页面。
我试过导航到重写的网站localhost/index.php?login
,所有的 CSS 和 HTML 都显示得很好。只有当我重写它时它localhost/login
才会失败。它显示了我的主页,没有任何应该包含在 index.php 中的样式
这是我的 .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
DirectoryIndex index.php
RewriteRule ^login$ index.php?login [NC]
</IfModule>
这是我的 index.php
//Check for more GET variables.
$url = $_SERVER['REQUEST_URI'];
$split = explode("?", $url);
if (isset($split[1])) {
$newvarsarray = explode("&", $split[1]);
foreach ($newvarsarray as $newvar) {
$keyandvalue = explode("=", $newvar);
if (isset($keyandvalue[1])) {
$_GET[$keyandvalue[0]] = $keyandvalue[1];
} else {
$_GET[$keyandvalue[0]] = '';
}
}
} else {
//No additional vars, leave $_GET alone.
}
include_once 'globalheader.html';//Imports styles and things
include_once 'header.html';//Stylized header
//This is the area where we handle the different states of the webpage and import them into here
if (empty($_GET)) {
//Main homepage
include_once 'mainpage.html';
} else if (isset($_GET['login'])) {
//Login page
include_once 'login.html';
} else if (isset($_GET['register'])) {
//Register page
include_once 'register.html';
} else if (isset($_GET['profile'])) {
//Profile page
}
include_once 'footer.html';//Stylized footer
include_once 'globalfooter.html';//Closes things from globalheader.html
?>
我希望它使用正确的 CSS 显示正确的页面,而不是看似跳过整个 index.php 并仅使用 .html 文件。
解决方案
tl;博士你失踪了[OR]
。
一个文件不能同时是“既不是文件也不是目录”。
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login$ index.php?login [NC]
</IfModule>
推荐阅读
- python - Python - PyQt5 正确使用线程
- python - 使用 Flask 和 Google Colab 渲染 html 模板时出错
- elasticsearch - 模式捕获不适用于正斜杠
- powershell - 导出 .CSV 文件,更改与导入相同的名称和文件夹
- encryption - 使用 ColdFusion 调用 Sage Pay API 时出错
- laravel - 如何使用 eloquent laravel 过滤两个日期之间的数据?
- java - 活动随时可用
- reactjs - 如何在 JEST 中测试 Window.postMessage()?
- javascript - IndexedDB - 如何在对象存储中读取最大 ID
- python - 根据索引从一列中选择第一个值,从第二个中选择最后一个值