使用htaccess重写规则访问相对的CSS/JS文件 [英] Access relative CSS/JS files with htaccess rewrite rule

查看:97
本文介绍了使用htaccess重写规则访问相对的CSS/JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近有人问我是否可以使我的朋友服务器地址更加用户友好.他当前的网址如下:

I was recently asked if I could make my friends server address more user friendly. His current urls looks like this:

http://wwww.example.com/site/index.php?page=home
http://wwww.example.com/site/index.php?page=about/john
http://wwww.example.com/site/index.php?page=portfolio/concept-art/2013

他希望他们看起来像这样

He would like them to look like this

http://wwww.example.com/site/home
http://wwww.example.com/site/about/john
http://wwww.example.com/site/portfolio/concept-art/2013

我以为这很简单,所以我在重写后写了这个.

which I thought would be pretty simple so I wrote this following rewrite.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^([^_]*)$ index.php?page=$1 [L]

似乎适用于基本链接

http://wwww.example.com/site/home

但是类似这样的东西

http://wwww.example.com/site/about/john

css或js均不会加载.因此,我现在通过将所有文件都设置为绝对路径来解决该问题,但我担心我的朋友将添加一个新插件或其他内容,而忘记了他必须将其设置为绝对路径.

none of the css or js will load. So I fixed that for now by making all of the files absolute paths but I am worried that my friend is going to add a new plugin or something and forget that he has to make it an absolute path.

我的问题

My Question

是否可以更改或添加htaccess文件以使css和js文件通过相对路径加载?如果是这样,我该怎么办?

Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?

推荐答案

更好地使用此规则:

RewriteEngine On
RewriteBase /site/

RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]

然后对css/js/images更好地在css,js,图像文件中使用绝对路径,而不要使用相对路径.这意味着您必须确保这些文件的路径以http://或斜杠/开头.

Then for css/js/images better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

或者,您可以尝试将其添加到页面标题中:

Alternatively You can try adding this in your page's header:

<base href="/" />

OR

<base href="http://domain.com/site/" />

这篇关于使用htaccess重写规则访问相对的CSS/JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆