.htaccess RewriteRule 将驱动器路径添加到 URL

发布于 2024-11-03 05:47:34 字数 1001 浏览 0 评论 0原文

我正在使用安装在 Win7 计算机上的 C: 上的 Zend Server CE (v.5.1.0)。我已将一个项目添加到 httpd.conf 中,方法是添加:

Alias /project "D:\Homepages\project"
<Directory "D:\Homepages\project">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
</Directory>

project 目录中的 .htaccess 文件包含以下内容:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/\w*\.(css|js) [NC]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

现在解决问题;如果我去

http://localhost/project/index.php

一切似乎都工作正常。我到达 index.php 文件并获取我的内容。 但是,如果我转到任何其他会触发 RewriteRule 的页面,它似乎会添加目录路径。 FireFox 输出以下未找到消息:

在此服务器上找不到请求的 URL /Homepages/project/index.php。

我试图在这里找到类似的问题/答案,但失败了。有什么想法吗? 诗。我接受答案可能会延迟,因为我要出去办事一段时间。

I am using Zend Server CE (v.5.1.0) installed on C: on a Win7 machine. I have added one project to httpd.conf by adding:

Alias /project "D:\Homepages\project"
<Directory "D:\Homepages\project">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
</Directory>

My .htaccess file in the project directory contains the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/\w*\.(css|js) [NC]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Now to the problem; if I go to

http://localhost/project/index.php

everything seems to be working fine. I reach the index.php file and get my contents.
However, if I go to any other page that would trigger the RewriteRule, it seems to be adding the directory path. FireFox outputs the following Not Found message:

The requested URL /Homepages/project/index.php was not found on this server.

I tried to find a similar question/answer here, but failed. Any idea?
Ps. Me accepting of an answer might be delayed as I will be out for a while on an errand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

路还长,别太狂 2024-11-10 05:47:34

您需要设置RewriteBase指令;否则,mod_rewrite 会自动并默认将文件路径添加到生成的重写规则中。

来自: http://httpd.apache.org/docs/2.0/mod/mod_rewrite .html

当发生新 URL 替换时,该模块必须将 URL 重新注入服务器处理中。为了能够做到这一点,它需要知道相应的 URL 前缀或 URL 基是什么。默认情况下,此前缀是相应的文件路径本身。然而,对于大多数网站来说,URL 与物理文件名路径并不直接相关,因此这种假设通常是错误的!因此,您可以使用 RewriteBase 指令来指定正确的 URL 前缀。
如果您的网络服务器的 URL 与物理文件路径不直接相关,则您需要在每个要使用 RewriteRule 指令的 .htaccess 文件中使用 RewriteBase。

You need to set the RewriteBase directive; otherwise, mod_rewrite automatically, and by default, prepends the file path to the resulting rewrite rule.

From: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix.
If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.

北方。的韩爷 2024-11-10 05:47:34

你的最后一行是这样的:

RewriteRule ^.*$ /index.php [NC,L]

但是我认为这是无限循环,所以我建议改用这个规则:

RewriteCond %{THE_REQUEST} !\s/index.php [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule . /index.php [L]

如果它已经是/index.php,它会阻止转到index.php。

Have your last line like this:

RewriteRule ^.*$ /index.php [NC,L]

However I think this is infinite loop so I would suggest this rule instead:

RewriteCond %{THE_REQUEST} !\s/index.php [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule . /index.php [L]

which prevents going to index.php if it is already /index.php.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文