mod_rewrite 和文件路径
我在 haccess 中使用 mod_rewrite 来创建干净的 URL。工作目录是 webroot/subdir
,htaccess 位于 subdir
中,
我的主文件夹工作正常,
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^projects/([0-9]+)$ ?action=projects&id=$1
RewriteRule ^projects ?action=projects
RewriteRule ^clients ?action=clients
RewriteRule ^admins ?action=admins
RewriteRule ^settings ?action=settings
因此这些链接就像 webroot/subdir/projects< /code> 或
webroot/subdir/settings
并且它们可以工作。
我的问题发生在 projects
行,我必须在其中添加 id。当我单击类似以下内容的链接时:webroot/subdir/projects/284
,我的所有样式表和图像都会损坏。我的 CSS 设置如下:
<link rel="stylesheet" href="resources/css/reset.css" />
<link rel="stylesheet" href="resources/css/main.css" />
<link rel="stylesheet" href="resources/css/buttonPro.css" />
有办法解决这个问题吗?我的重写规则错了吗?
I am using mod_rewrite in my haccess to make clean URLs. The working directory is webroot/subdir
and htaccess resides in subdir
What I have works fine for the main folder
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^projects/([0-9]+)$ ?action=projects&id=$1
RewriteRule ^projects ?action=projects
RewriteRule ^clients ?action=clients
RewriteRule ^admins ?action=admins
RewriteRule ^settings ?action=settings
so those links would be like webroot/subdir/projects
or webroot/subdir/settings
and they work.
My problem occurs on the projects
line where I have to add an id. When I click a link that says something like: webroot/subdir/projects/284
, all my style sheets and images break. My CSS is setup like so:
<link rel="stylesheet" href="resources/css/reset.css" />
<link rel="stylesheet" href="resources/css/main.css" />
<link rel="stylesheet" href="resources/css/buttonPro.css" />
Is there a way around this? Am I doing my rewrite rules wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的
resources
文件夹也在webroot/subdir
中,您需要将链接设置为绝对路径。当页面更改为projects/([0-9]+)
时,您的工作目录现在变为webroot/subdir/projects
即使路径正在被重写,因此正在尝试在webroot/subdir/projects/resources
查找不存在的样式表。在位置的开头添加
/
应该可以:如果您的资源文件夹位于 home/root 目录之外,则需要指定该目录的完整路径,如下所示:
Assuming that your
resources
folder is also inwebroot/subdir
, you need to make your links absolute paths. When the page changes toprojects/([0-9]+)
, your working directory now becomeswebroot/subdir/projects
even though the path is being rewritten, so it is trying to look up your style sheets atwebroot/subdir/projects/resources
which doesn't exist.Adding a
/
to the beginning of your locations should work:If your resources folder is outside the home/root directory, you will need to specify the full path to that directory, like this: