开发者

Is it OK to use resources (such as CSS stylesheets and images) which are redirected by the same domain/host?

开发者 https://www.devze.com 2023-02-19 04:41 出处:网络
I\'m developing a web application where I can easily convert Static HTML pages into Dynamic HTML and I want to do it without applying any unnecessary configurations. For manageability purposes, I\'m p

I'm developing a web application where I can easily convert Static HTML pages into Dynamic HTML and I want to do it without applying any unnecessary configurations. For manageability purposes, I'm placing the Static HTML files in a subdirectory:

/开发者_JAVA技巧root/content/theme/

In the /root/ directory is where the index.php file resides. Let's say for example, a Static HTML file index.html is inside the /root/content/theme/ subdirectory. With the help of Apache's Rewrite Module, whenever I visit http://root.com/index.html, it will include /index.html from the /root/content/theme/ subdirectory, therefore it would appear that I am accessing the /index.html. Now the problem is, though the /index.html is loaded, but the resources like images and stylesheets (all URLs are relative) would not be properly loaded. My solution for this is to use redirects.

All page requests would have to be filtered in /index.php (of course those which do not actually exist in the server since it's aided with mod_rewrite). If an image resource would have to be requested from the server, like "style/images/bg.png", since this is a relative URL, it means it is requesting for "http://root.com/style/images/bg.png". Of course, this page does not exist because the real bg.png exists in "http://root.com/content/theme/style/images/bg.png". So whenever index.php detects that a resource in the /root/content/theme/ is being requested, it would just have to be redirected to "http://root.com/content/theme/style/images/bg.png".

This solution works fine, but what I'm worried about is if this requires more server load and would be not recommended, since every resource in the /root/content/theme/ would have to be redirected from the root.

I was thinking maybe this won't really matter that much since most browsers have cache system. What do you think?

By the way, I am using relative URLs for the resources because I wanted the application to be as flexible as possible (like when I needed to migrate my files to another domain)


I think I figured out a better solution for this, also using RewriteModule

RewriteCond %{DOCUMENT_ROOT}/content/theme/$1 -f
RewriteRule ^(.*)$ /content/theme/$1 [QSA,L]

Since when I'm redirecting through index.php, it must check first if the file exists, it might take more server load because PHP is still being parsed.. The above .htaccess lines would do the same solution, but this time, a lot faster


That's a common problem, and it's not solvable without some effort. HTML however provides for a simple workaround, namely <base href=...>.

It can help with using primarily relative paths and avoids micromanaging redirects. I'm not quite sure about your case (directory structures are always hard to imagine without shiny treeview), but it can be utilized most easily from any PHP template:

<base href="<?=htmlspecialchars("//$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", ENT_QUOTES, "UTF-8")?>" />

Now I would personally make some URL paths absolute rather, and only use the mod_rewrite solutions for the edge cases. But it all hinges on if you can actually use PHP in your templates.

0

精彩评论

暂无评论...
验证码 换一张
取 消