6

I use a .htaccess file to redirect all requests to the same index.php. From there, I decide what to do depending on URL of the incoming request.

For now, I map the request URL to directory structure relative to a source folder, so that example.com/user/create maps to source/user/create/index.php. The file located there can handle the request as needed, and generate HTML output.

But that doesn't work for static assets, the browser may request. So my idea is to find out whether a request URL ends with a file extension and map that to a directory structure relative to a assets folder, so that example.com/css/default.css would map to assets/css/default.css. But I don't know how to respond with a static file instead of HTML code from PHP.

Normally, this might be done using an additional .htaccess rule, but I'd like to change the assets folder dynamically in code.

How can I send a static file, given on the server's hard drive, to the browser client in PHP?

danijar
  • 32,406
  • 45
  • 166
  • 297
  • You can render assets (e.g. CSS files) using PHP, but it's much slower than serving files statically. It's usually better to rebuild your CSS if/when it changes. – halfer Nov 03 '13 at 13:16
  • It's still quite unclear, but i'd suggest to look into the routing component of symfony2: with that you can "attach" some request urls to some controllers who handle the response,therefore serving the content you need ( http://symfony.com/doc/current/components/routing/introduction.html ) – Stormsson Nov 03 '13 at 13:22
  • 1
    I updated my question and hope my intention is clearer now. – danijar Nov 03 '13 at 13:32

2 Answers2

3

Well, if you really want to, you can just use readfile(). But it would be better to let the web server handle static files if you can; it's more efficient than firing up PHP just to throw a file out the door.

Also, with the PHP approach you'll probably have to set the Content-Type header appropriate per file, which can be annoying. Your web server will be doing that for you already when serving files directly.

Plus there are other things that you may be getting for "free" at the moment that you'll have to consider -- using ob_gzhandler if you want to compress your pages, as might already be being done for static files by Apache, say.

So, while it's possible, and I can see reasons why it's sometimes desirable, I'd probably try to make this kind of processing the exception rather than the rule for files that aren't generally dynamic...

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
2

On the the htacess

#Expections
RewriteCond %{REQUEST_URI} ^/assets.*$
RewriteRule ^(.*)$ - [L,NC]

#You redirect to index
...

put a / at the beginning of the file path

/assets/path/to/file
Guilherme Soares
  • 726
  • 3
  • 7
  • 19
  • Is there a way to dynamically change the `assets` directory in PHP? Anyway, I will use your solution. – danijar Nov 03 '13 at 18:28
  • I think that only if you change .htaccess – Guilherme Soares Nov 03 '13 at 19:50
  • Alright, I'll go with the `.htaccess`. I have one question about your example. Wouldn't it match any urls with dots inside? I don't want to match urls like `example.com/path.with.dots/page`. Thanks a lot for your answer! – danijar Nov 30 '13 at 13:16
  • if you put at the .htaccess the rules rewritecond of folder with dots you need escape the dots( put \ before dot) – Guilherme Soares Nov 30 '13 at 19:43
  • I am not sure if I understood you right. I cannot make a rule for every url with dots. The distinction is that those non asset urls have slashes after the dots. In other words, the last url segment does only contain dots in asset urls. – danijar Nov 30 '13 at 20:04
  • sorry for my english I am not good. Then i spoken that you need put a slash before dots in case if you use folder with dots. if you don't escape the dot "." at regex it is equal a any char. Example: ^/same.test.*$ if you create folder same.test or sameWtest you can access. but if the regex is equal: ^/same\.test.*$ only the folder same.test can be access – Guilherme Soares Nov 30 '13 at 20:17
  • So I would need something like `.+\.[^/]+` to find asset requests. I think that would match urls that start with *anything*, include *a dot*, and then end with *anything, but no more slashes*. How would the according `.htaccess` look like then? – danijar Nov 30 '13 at 22:58
  • I think that you don't need use this regex .+\.[^/] because all i can access all files from folder assets. you can put some folder that contains other this folders with dots. RewriteCond %{REQUEST_URI} ^/examples.*$\ all folder or files in examples can be access.. – Guilherme Soares Nov 30 '13 at 23:33
  • Alright. One last question about this. Is it possible to add the `/assets/` folder as a prefix in `.htaccess` so that it doesn't need to be stated in the url? – danijar Dec 01 '13 at 01:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42260/discussion-between-guilherme-soares-and-danijar) – Guilherme Soares Dec 01 '13 at 02:17