user4060842
user4060842

Reputation:

I am not able to access my css & js files when i use rewrite url

Before using rewrite url every this diplayed correctly

without using rewrite url

but when i used rewrite url

http://localhost/mgosoft/admin/userreg/

when used rewite url

please help me out with this thing thank you :)

Upvotes: 0

Views: 85

Answers (3)

O-mkar
O-mkar

Reputation: 5658

You can also try this For your local version add

<base href="//localhost/mywebsite" />

to the head section

and for your live versions change it to

<base href="//your.domain.here" />

reference at http://www.w3.org/TR/html4/struct/links.html#h-12.4

Upvotes: 0

dognose
dognose

Reputation: 20899

@O-mkar nailed it pretty much. Another thing you can do - if you don't want to change all references through your whole project: rewrite them to the correct URL automatically using another htaccess line (for /css/ directory)::

RewriteCond %{REQUEST_URI} !^/css/ [NC]
RewriteRule ^.*/css/([^/]+)$ /css/$1 [L, R=301]

Thís will rewrite any wrong url like http://localhost/mode/movies/view/profile/css/style.css

to the fixed resource path http://localhost/css/style.css

ofc. localhost needs to match the proper external hostname.

depending on how generic your original rewrite is, you should exclude resource paths from beeing rewritten again, or you end up with an infinite rewrite loop:

RewriteCond %{REQUEST_URI} !^/css/ [NC]
RewriteCond %{REQUEST_URI} !^/images/ [NC]
RewriteCond %{REQUEST_URI} !^/javascript/ [NC]

etc...


As of your example given and the resource file is assets/css/themes/simple/style.css

You basically need to say: Rewrite anything that ENDS with this String to the root folder. Thus, it would be:

RewriteCond %{REQUEST_URI} !^/assets/css/themes/simple/ [NC] #skip, if already rewritten
RewriteRule ^.*/assets/css/themes/simple/([^/]+)$ /assets/css/themes/simple/$1 [L, R=301]

You can use this nice page to play around: http://htaccess.madewithlove.be/

put in the above example and the request url http://localhost/mode/movies/assets/css/themes/simple/style.css to see, it will become http://localhost/assets/css/themes/simple/style.css

ofc. you can play arround and redirect EVERY subfile of /assets/ like that, then you dont need to create rules per file.

ps.: If you are using [L, R=301] to redirect resources, the browser cache will work as well, cause it will remember the permanent move of the file requested. - without R=301 it will think that's different files all the time.

Upvotes: 1

Vinit Chouhan
Vinit Chouhan

Reputation: 686

As you are using php just use $site_addr = $_SERVER['HTTP_HOST']; and append this before all your links like->

<link rel="stylesheet" type="text/css" href="<?php echo $site_addr; ?>css/style.css">

Hope it will help

Upvotes: 1

Related Questions