duanao4729 2019-04-15 13:32
浏览 155
已采纳

如何修复.htaccess重写不显示CSS [重复]

This question already has an answer here:

I'm running a localhost on my Mac using the native sites, and I can't seem to get .htaccess CSS working. I'm using an index.php page, that is then given url variables to tell which page to display (ex. index.php?login). There two issues I'm facing here: The first is that it redirects to a page where my global styles aren't included, and the second is that it shows the homepage instead of my login page.

I've tried navigating to the rewritten site as localhost/index.php?login, and all the CSS and HTML displays fine. It's only when I rewrite it as localhost/login that it fails. It shows my homepage without any of the styles that should be include from index.php

Here is my .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>

Here is my 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
?>

I want it to display the correct page with correct CSS instead of seemingly skipping over the entirety of index.php and just using the .html file.

</div>
  • 写回答

1条回答 默认 最新

  • doudai3012 2019-04-15 13:36
    关注

    tl;dr you are missing [OR].

    A file cannot be "not a file nor a directory" at the same time.

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?