我最近开始重写.htaccess url,目前我面临一个问题。
我的文件夹架构是这样的:
- index.php
- .htaccess
- 图像/
- css/
- js/
- 函数/
我的网站通过加载所有页面
index.php?page=register
我的页面在功能图中的位置。
问题是,当我把这个放在我的.htaccess 中时
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^register/?$ index.php?page=register [NC,L] # Handle login page request
RewriteRule ^profile/([0-9]+)/?$ index.php?page=profile&member_id=$1 [NC,L] # Handle profile page request
当我导航到时
<website>/register/
我的CSS文件位于
../css/style.css
但当我导航到时
<website>/profile/1/
我的css文件位于
../../css/custom.css
如何确保页面始终从加载我的文件
css/custom.css
是否有一个变量提到了index.php的位置?这样我就可以做
<link rel="stylesheet" type="text/css" href="<?= $variable ?>/css/custom.css" />
我在Windows上使用XAMPP(最新版本),该网站将在基于Linux的网络服务器上发布。
如果您需要更多关于我的实现的信息,而我现在无法思考,我会与您分享。
有两种方法可以解决这个问题:
-
在css、js和图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以
http://
或斜线/
开头。 -
您也可以尝试将其添加到页面的HTML标题中:
<base href="/" />
,以便从该URL而不是当前URL解析每个相对URL。
如果您的站点位于basedir中,例如http://example.com/
,则只执行
<link rel="stylesheet" type="text/css" href="/css/custom.css" />
如果它位于像http://example.com/mysite/
这样的子目录中,那么你必须将它添加到你的路径中
<link rel="stylesheet" type="text/css" href="/mysite/css/custom.css" />
您应该在.htaccess
中添加以下内容
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
如果存在文件,这将"忽略"重写规则,以便加载css。