mod_rewrite 和文件路径

发布于 2024-12-02 21:16:56 字数 913 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对不⑦ 2024-12-09 21:16:56

假设您的 resources 文件夹也在 webroot/subdir 中,您需要将链接设置为绝对路径。当页面更改为 projects/([0-9]+) 时,您的工作目录现在变为 webroot/subdir/projects 即使路径正在被重写,因此正在尝试在 webroot/subdir/projects/resources 查找不存在的样式表。

在位置的开头添加 / 应该可以:

<link rel="stylesheet" href="/resources/css/reset.css" />
<link rel="stylesheet" href="/resources/css/main.css" />
<link rel="stylesheet" href="/resources/css/buttonPro.css" />

如果您的资源文件夹位于 home/root 目录之外,则需要指定该目录的完整路径,如下所示:

<link rel="stylesheet" href="/webroot/subdir/resources/css/reset.css" />
<link rel="stylesheet" href="/webroot/subdir/resources/css/main.css" />
<link rel="stylesheet" href="/webroot/subdir/resources/css/buttonPro.css" />

Assuming that your resources folder is also in webroot/subdir, you need to make your links absolute paths. When the page changes to projects/([0-9]+), your working directory now becomes webroot/subdir/projects even though the path is being rewritten, so it is trying to look up your style sheets at webroot/subdir/projects/resources which doesn't exist.

Adding a / to the beginning of your locations should work:

<link rel="stylesheet" href="/resources/css/reset.css" />
<link rel="stylesheet" href="/resources/css/main.css" />
<link rel="stylesheet" href="/resources/css/buttonPro.css" />

If your resources folder is outside the home/root directory, you will need to specify the full path to that directory, like this:

<link rel="stylesheet" href="/webroot/subdir/resources/css/reset.css" />
<link rel="stylesheet" href="/webroot/subdir/resources/css/main.css" />
<link rel="stylesheet" href="/webroot/subdir/resources/css/buttonPro.css" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文