Forum Moderators: phranque

Message Too Old, No Replies

Url Rewriting that affects js files and css

         

cfmtravel

5:45 pm on Jan 6, 2014 (gmt 0)

10+ Year Member



I worked with simple rules in the htaccess file for quite some time and I've never had any kind of problem.
But all the previous time I used in the rewriting rules different variables (both numeric and string) and the file extension (.php) at the end of the address.

This time I'm trying to rewrite using the form site.com/new-name-of-the-page/

And I realized that the something happen with the js and css files. I'm not sure how to explain but the js files don't work anymore after I upload the htaccess file.

RewriteEngine On

RewriteRule ^events-in-([^/]+)/$ levelone.php?lv1=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/]+)/([^/]+)/$ leveltwo.php?lv1=$1&org=$2&%{QUERY_STRING} [L]

Do I have to insert as variable the numeric ID and the extension .php?

g1smd

6:00 pm on Jan 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure the links to the css and js files begin

href="/


and include the full path to the file.

lucy24

9:43 pm on Jan 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Long version:

The browser doesn't know it has been rewritten. That's the essence of a rewrite. So if the browser "thinks" it is at
example.com/directory/pagename

while it is "really" --after rewriting-- at
example.com/otherpage.php?blahblah

then if the page specifies
"stylename.css"

the browser will put in a request for
example.com/directory/stylename.css

even if the styles really live at
example.com/stylename.css

See how that works? The solution is to use only site-absolute URLs --the ones that begin in a / slash-- for all supporting files. Images, stylesheets, javascript and so on. Anything that's referenced by the page.

Incidentally, the same thing applies to any supporting files used by error documents (404, 403 etc).

cfmtravel

3:57 pm on Jan 7, 2014 (gmt 0)

10+ Year Member



Thanks guys
So as a rule of thumb

in every page where I use JS, CSS, IMG (or other supporting files such as PDF)

I have to use absolute URL
<img src="/img/namefile.png" />
<link href="/css/main.css" rel="stylesheet" />
<script src="/js/bootstrap.min.js"></script>

Thanks a lot

JD_Toims

4:36 pm on Jan 7, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes -- I also usually eliminate those file types from all rewrites/redirects, unless I know I need to redirect them for some reason, so one of my first rules is almost always something like:

RewriteRule \.(js|png|gif|jpe?g|css|txt|ico) - [L]

cfmtravel

5:18 pm on Jan 7, 2014 (gmt 0)

10+ Year Member



good point

thanks