【问题标题】:How to fix .htaccess rewrite not showing CSS [duplicate]如何修复.htaccess重写不显示CSS [重复]
【发布时间】:2019-04-15 13:32:41
【问题描述】:

我正在使用本地站点在我的 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 文件。

【问题讨论】:

    标签: php html .htaccess


    【解决方案1】:

    tl;dr 你错过了[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>
    

    【讨论】:

    • 感谢您的快速回复!这使得登录页面 HTML 正确显示,但全局 CSS 仍然不想显示。
    • 没有看到提出了什么请求就忍不住了。
    • 这是我的全局样式导入:&lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;!--Font imports--&gt; &lt;link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"&gt; &lt;!--Global css imports--&gt; &lt;link rel="stylesheet" type="text/css" href="http://localhost/normalize.css"&gt; &lt;link rel="stylesheet" type="text/css" href="http://localhost/globalstyle.css"&gt; &lt;/href&gt;
    猜你喜欢
    • 2019-12-16
    • 2023-03-23
    • 2017-03-26
    • 2019-09-08
    • 1970-01-01
    • 2014-10-07
    • 2021-08-07
    • 2013-10-30
    相关资源
    最近更新 更多