Php 使用htaccess重写规则访问相关CSS/JS文件

Php 使用htaccess重写规则访问相关CSS/JS文件,php,regex,.htaccess,mod-rewrite,Php,Regex,.htaccess,Mod Rewrite,最近有人问我是否可以让我的好友服务器地址更方便用户使用。他当前的URL如下所示: 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 他希望他们看起来像这样 http://wwww.example.c

最近有人问我是否可以让我的好友服务器地址更方便用户使用。他当前的URL如下所示:

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
他希望他们看起来像这样

http://wwww.example.com/site/home
http://wwww.example.com/site/about/john
http://wwww.example.com/site/portfolio/concept-art/2013
http://wwww.example.com/site/about/john
我觉得这很简单,所以我写了下面的重写

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/home
http://wwww.example.com/site/about/john
http://wwww.example.com/site/portfolio/concept-art/2013
http://wwww.example.com/site/about/john
css或js都不会加载。所以我现在通过使所有文件都成为绝对路径来解决这个问题,但我担心我的朋友会添加一个新插件或其他东西,而忘记了他必须使其成为绝对路径

我的问题


我是否可以在我的htaccess文件中更改或添加一些内容,以使用相对路径加载css和js文件?如果是,我需要做什么?

最好使用此规则:

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、images文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以
http://
或斜杠
/
开头

或者您可以尝试在页面标题中添加以下内容:

<base href="/" />


我是否可以在我的htaccess文件中更改或添加一些内容,以使用相对路径加载css和js文件?如果是这样,我需要做什么

您可以在页面标题中添加适当的相对URI基:

<base href="/site/" />

我简直不敢相信我竟然没有想到
base
总是忘记基本的HTML解决方案。哈哈。我添加了一个php函数来自动生成基础,以防他移动他的站点,但除此之外,它工作得非常好。我总是忘记尝试基本的html解决方案,如
base
。哈哈。