yogaboyz88
yogaboyz88

Reputation: 39

Can't load css, js and image with htaccess

I have problems in using the .htaccess file. css, js and image can't load if i use .htaccess file.

this my structure simple web. I have some files and folders in the root directory :

1 css folder (include style.css).

@charset "utf-8";
/* CSS Document */

h1 {
    color:#F00;
}

1 index.php file

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Clean URL</title>
<link href="css/styles.css" rel="stylesheet" /> 
</head> 
<body>
<?php include "bukafile.php"; ?>
</body>
</head> 

1 bukafile.php file

<?php
switch ($_GET['page']){
case '' : if(!file_exists ("home.php"))
die ("File Not Found");
include "home.php";
break;
case 'artikel' : if(!file_exists ("artikel.php"))
die ("File Not Found");
include "artikel.php";
break;
default: break;
}
?>

1 home.php file

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Clean URL</title> 
</head> 
<body>
<?php
$sql=mysql_query("SELECT * FROM artikel order by id_artikel");
while ($r=mysql_fetch_array($sql)) {
$judul2 = str_replace(" ","-",$r[judul]);
?>

<p><?php echo"Judul : $r[judul]"; ?></p>
<p><?php echo"<a href='artikel/$r[id_artikel]/$judul2'>detail artikel</a>"; ?></p>

<?php
}
?>
</body>
</head>

1 artikel.php file

<!DOCTYPE HTML> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>Clean URL</title> 
    </head> 
    <body>
<h1>Percobaan</h1>
<?php


$detail=mysql_query("SELECT * FROM artikel WHERE id_artikel='$_GET[id]'");
$r = mysql_fetch_array($detail);

echo "<p>$r[judul]</p>";
echo "<p>$r[content]</p>";

?>
</body>
    </head>

1 .htaccess file

RewriteEngine on

RewriteRule ^artikel/([0-9]+)/([A-Za-z0-9-]+)/?$ index.php?page=artikel&id=$1 [NC,L] 

In this case, css file can't load in artikel php (tag h1) if the source code <link href="css/styles.css" rel="stylesheet" /> but if i change source code <link href="../../css/styles.css" rel="stylesheet" /> it's running well.. so, how can I edit the file htacess without making changes to the source code css?? thank's for the answer

Upvotes: 2

Views: 2422

Answers (2)

rafa_pe
rafa_pe

Reputation: 217

You can define the base in the .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteBase /name of the path base

read this post too my htaccess file doesn't work to load css

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143956

This is because you've changed your relative URI base. When the browser loads:

/index.php?page=something

The base is /. But when the browser loads:

/artikel/123/abcd

The base is /artikel/123/. That's what happens when you relative URLs. The browser has no idea that your files are actually somewhere else or that the URL will be rewritten. So you need to tell the browser what the base is.

Try adding this in the header of your pages:

<base href="/" />

Upvotes: 3

Related Questions