0

A little question about using htaccess to rewrite uri's and also take care of the fact that browsers requests for included files (like .css files) are "wrong".

First of all, the background: i have this file structure on the server:

/mysite/.htaccess
/mysite/articles.php
/mysite/includes/css-files/layout.css

and into articles.php i have the lines

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

everything should work well, when i directly request the php file. But, since we don't want to request it directly, but using the rewrite rules to make the url user/ceo-friendly, we run into a problem with relative paths. So, for example, having these lines into the .htaccess file

# take care of different requests
RewriteRule ^articles/writer/(\w*)/?$ articles.php?writer=$1
RewriteRule ^articles/tag/(\w*)/?$ articles.php?tag=$1

when the user goes to mysite/articles/writer/erenor, the server redirects the request to mysite/articles.php?writer=erenor. This is good. Then, the browser tries to request the css file, but the request will be the following:

GET mysite/articles/writer/includes/css-files/layout.css

And this is not good. So, i tried to add a rule like this:

# take care of adapting "base url" for css files
RewriteRule /(\w*)\.css includes/css-files/$1.css

It seems to work, and the file is requested from the correct location.

Now, the question(s): is this a correct approach? Could i incur in security issues? Is there something better out in the programmer's world?

Thanks for your support.

1 Answer 1

2

The approach is probably less desirable as you now have 2 URL's that point to the same resource. Something that you can do is redirect instead of rewrite:

RewriteRule /(\w*)\.css includes/css-files/$1.css [L,R=301]

But the source of your problem is this:

@import url(./includes/css-files/layout.css);

There's a . in front of the /includes which makes it a relative URL, and the browser has to determine what the URI base to use when it goes to request layout.css. As far as it knows, the resource it requested is at /mysite/articles/writer/erenor, so obviously it's going to assume the base is /mysite/articles/writer/. It doesn't know anything about your rewrites and it's doing things the correct way. To avoid this, either change your URLs to be absolute:

@import url(/mysite/includes/css-files/layout.css);

or add the correct relative URI base to the page's header:

<base href="/mysite/" />
Sign up to request clarification or add additional context in comments.

3 Comments

Well, the two urls are just an example, and they may (should) be pointing to different files. The idea of redirecting css request is not bad..but could it get into troubles when i have a lot of "permanent redirects" to the same css file (since i expect to have a lot or articles and writers, for example)? Finally, the issue about the "dot" that makes the path relative is because i want to be able to move the site in a different location without going crazy modifying all the files (right now this test-site is in a sub-dir of main site)
@ErenorPaz if you're rewriting the URI to have multiple levels of directories, there's no way around it. You're going to have to specify what the URI base is somewhere, it's just up to you where you want to do it; in the page content or in the htaccess file. The permanent redirects are best because they can be cached. It's not a problem if a million different URLs redirect to a single resource because that's where it really is and the 301 tells the user agent that. If you're worried about permanent, you can remove the =301 bit to make it temporary.
Oh oh oh. That's where i was missing something: the base url for links, not only css files. So, the general idea is ok, but it's not complete. I'll go with URI base into htaccess. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.