.htaccess - rewriting url requests and taking care of resource files

I thought i should open a new question for a matter related to my previous one (you can find it here). The situation is very similar to that question, but i found a different approach, and would like to know if it's correct.

I wanted to rewrite the urls for my site, and being able to take care of resource files (css, js, images, etc) so that browser doenn't search for them in the wrong directory.

Following are snippets of the html code of a sample page found browsing for example to articles/writer/erenor (here htaccess rewrites the url to /articles.php?writer=erenor, and this part works well).

Into <head> tag:

<script type="text/javascript" src="./inc-javascript-files/jquery.js"></script>

<style type="text/css">
  @import url(./inc-css-files/index.css);
</style>

Into <body> tag:

<img alt="Avatar" src="./inc-images-files/avatar.png">
<img alt="Pattern" src="./inc-images-files/pattern/violet.png">
<br><br>
<a href="/articles/writer/erenor">Writer Erenor</a>

Now, i have this snippet from .htaccess file:

# take care of resource files
RewriteRule inc\-(css|javascript|images)\-files/(.*)\.(png|jp[e]g|gif|js|css) includes-$1/$2.$3 [L]

# url rewriting
RewriteRule ^articles/writer/(\w*)/?$ articles.php?writer=$1

And, finally, the example of the file structure:

/mysite
/mysite/.htaccess    //this is the htaccess file we are talking about ;-)
/mysite/articles.php
/mysite/includes-css
/mysite/includes-css/index.css
/mysite/includes-javascript
/mysite/includes-javascript/jquery.js
/mysite/includes-images
/mysite/includes-images/avatar.png
/mysite/includes-images/pattern
/mysite/includes-images/pattern/violet.png

I just tested the code, and it seems to work: my browser asks for the css file (searching it in the "wrong" place) and it retrieves it correctly, so i'm quite happy with it :) Links in the page will be like the one in the html shown above, which seems to work well.

Questions: 1. Is this a good approach to be able to avoid browsers requests to resources being in the "wrong" place? (I know that i will have to add more parameters when it comes to other files like for example videos, txt's, tga, etc etc etc) 2. Should i move this site to production server, will it work without modifications? In other words, is this a kind of "box" that can be moved here and there easily?

A little note: since these are just code snippets, tell me if something appears to be missing..maybe i just forgot to copy/paste it ;-)

Answers


You seem to be requesting the files in the current directory. Why do you do that? Why don't you request the from the site root? (remove the '.'). If I read it correctly, on /articles/writer/asdf and /articles/writer/zxcv the files will request /articles/writer/asdf/inc-css-files/index.css and /articles/writer/zxcv/inc-css-files/index.css. The browser sees this as two different files. If the user requests 100 writers, it will download index.css 100 times, and caches the same file 100 times under different names. That is wasteful.

I recommend requesting the files relative to the site root instead if you have the files stored in a folder in the site root.


Need Your Help

C++ Lambdas: captured pointer to STL container changing address after pop?

c++ pointers c++11 lambda

I'm trying to figure out why the following code breaks. After objects-&gt;pop(); is called in the lambda expression, the memory address of objects changes to 0xfeeefeee when debugging in Visual Stu...

Hibernate: org.hibernate.WrongClassException, SINGLE_TABLE inheritance and DiscriminatorFormula

java hibernate hsqldb single-table-inheritance

I'm using Hibernate 3.2.2 GA with HSQLDB 2.0 GA, and I have a class hierarchy similar to the following: