ashutosh
ashutosh

Reputation: 1232

CSS and JS not loading with mod_rewrite

I am using apache's mod_rewrite module to improve my URL structure(for one instance- www.mydomainname.com/user.php?u=somename) to some fancy URL like www.mydomainname.com/user/somename . I have successfully achieved it until I got into this very bad situation of CSS and JS relative path issue. Whenever I open up www.mydomainname.com/user/somename, it loads successfully without CSS and JS with only text and basic HTML. I used chrome's "inspect element" feature, which help me to know the following errors that occurred.

GET http://www.mydomainname.com/user/css/style.css 404 (Not Found) somename:5
GET http://www.mydomainname.com/user/css/developer.css 404 (Not Found) somename:6
GET http://www.mydomainname.com/user/js/jquery-1.5.2.js 404 (Not Found) somename:7
GET http://www.mydomainname.com/user/js/jquery.form.js 404 (Not Found) somename:5

The .htaccess file has following contents-

DirectoryIndex index.html.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule    ^user/([^/.]+)/?$    user.php?u=$1    [L]

The user.php is residing in the parent folder as well as the CSS and JS folders. I got to know the cause but do not know how to remove the errors.

EDIT: The path of CSS and JS given in the user.php file are-

<link type = "text/css" rel = "stylesheet" href = "css/style.css">
<link type = "text/css" rel = "stylesheet" href = "css/developer.css">
<script type="text/javascript" src="js/jquery-1.5.2.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>

Upvotes: 1

Views: 11259

Answers (3)

NarolaInfotech Demo
NarolaInfotech Demo

Reputation: 75

You dont need to worry about it. You can just open "httpd.conf" file from apache. and remove hash from below line.

Example:

#LoadModule rewrite_module modules/mod_rewrite.so

Change it it:

LoadModule rewrite_module modules/mod_rewrite.so

Upvotes: -2

Kami
Kami

Reputation: 19417

try

 DirectoryIndex index.html.php
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule    ^user/([^/.]+)/?$    user.php?u=$1    [L]

I believe you are allowing requests for file but not for directories. The JS/CSS files are in sub directories.

// UPDATE The relative paths your are using are the issue.

The code you have pasted for your paths, essentially says, Look for this css file in the sub folder of the current folder. The subfolder does not exist, hence the error.

Change your code to the following.

<link type = "text/css" rel = "stylesheet" href = "/css/style.css">
<link type = "text/css" rel = "stylesheet" href = "/css/developer.css">
<script type="text/javascript" src="/js/jquery-1.5.2.js"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>

This will translate to look for the files from the root folder.

Upvotes: 2

Korikulum
Korikulum

Reputation: 2599

Try adding absolute paths (just add / at the beginning of the URL):

<link type = "text/css" rel = "stylesheet" href = "/css/style.css">
<link type = "text/css" rel = "stylesheet" href = "/css/developer.css">
<script type="text/javascript" src="/js/jquery-1.5.2.js"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>

Upvotes: 7

Related Questions