Mod Rewrite Rule appending to css js and images paths

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frank_mark
Forum Newbie
Posts: 13
Joined: Fri Jan 16, 2009 6:35 am

Mod Rewrite Rule appending to css js and images paths

Post by frank_mark »

Hi all, I am having some problems with my implementation of rewrite rule.

I have the following code in my .htaccess:

Code: Select all

 
RewriteEngine On
RewriteBase /
RewriteRule ^food/([0-9]+)$ index.php?view=show_category&id=$1 [L]
RewriteRule ^vision$ index.php?view=vision [L]
 
following is my css and it is not in the document root (as is the product image folder and various includes):

Code: Select all

 
<link href='css/style.css' rel='stylesheet' type='text/css' />
 
Rewriting itself works perfectly fine, however, if I use the rewrite rule in line 5 the css is loaded perfectly, but if I use the rewrite rule in line 4 all my links to images, css, javascripts have an appended 'food/' to their path.

I have tried adding the front slash to the beginning of my css, however that seems to not make the css work on any rewrite rules. If I do add the entire absolute path to my css as such:

Code: Select all

 
<link href=http:/localhost/css/style.css' rel='stylesheet' type='text/css' />
 
it works fine. This is not a practical solution I'm guessing.

Anyone have any suggestions?? I would immensely appreciate it. Thanks!!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Mod Rewrite Rule appending to css js and images paths

Post by requinix »

Using /css/style.css (with the slash) should work perfectly fine. You say you tried that?

Code: Select all

<link href='/css/style.css' rel='stylesheet' type='text/css' />
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: Mod Rewrite Rule appending to css js and images paths

Post by wpsd2006 »

another stupid method but definitly work 100%

you write a full address for image css or even the script

<link href='http://www.mysite.com/css/style.css' rel='stylesheet' type='text/css' />

for image also src="http://www.mysite.com/css/image.jpg"

you can DEFINE the website address and apply it on the rest of image
so when you change your web name you only need to change in the DEFINE
oh and dont use the $_SERVER['DOCUMENT_ROOT'] its better using the web address
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Mod Rewrite Rule appending to css js and images paths

Post by requinix »

wpsd2006 wrote:another stupid method but definitly work 100%

you write a full address for image css or even the script
I seem to recall him saying something about "it works fine" but it's "not a practical solution".
(I agree.)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Mod Rewrite Rule appending to css js and images paths

Post by pickle »

What is an example of a fully qualified URL that you want rewritten (ie: http://www.example.com/food/99 ) ?
What is the fully qualified URL of your CSS (ie: http://www.example.com/css/style.css) ?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: Mod Rewrite Rule appending to css js and images paths

Post by wpsd2006 »

I seem to recall him saying something about "it works fine" but it's "not a practical solution".
(I agree.)
well it works both in my server and localhost ( sometime when i user e.g '/style.css' the '/' wont work in my localhost )
and fits nicely in every browser

no security vulnerability in this right i mean showing your http://website.com/bla bla bla
than showing the directory root of your server
frank_mark
Forum Newbie
Posts: 13
Joined: Fri Jan 16, 2009 6:35 am

Re: Mod Rewrite Rule appending to css js and images paths

Post by frank_mark »

Thanks for you help guys. Immensely appreciated. This forum by stretches of the universe has a better (in number and quality) response rate.

Much appreciated. It seems that when I uploaded to the live server for testing it somehow worked.

I am terrified because I have no clue what the heck happened.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Mod Rewrite Rule appending to css js and images paths

Post by s.dot »

What probably happened was / was not the intended directory in your localhost. You probably had the site in a subfolder such as /sitename/ and you needed /sitename/style.css.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply