【问题标题】:.htaccess RewriteRule adds drive path to URL.htaccess RewriteRule 将驱动器路径添加到 URL
【发布时间】:2011-08-12 07:54:41
【问题描述】:

我在 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 输出以下 Not Found 消息:

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

我试图在这里找到类似的问题/答案,但失败了。任何想法? 附言。我可能会延迟接受答复,因为我要外出一段时间。

【问题讨论】:

标签: apache .htaccess mod-rewrite apache2


【解决方案1】:

您需要设置 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。

【讨论】:

    【解决方案2】:

    最后一行是这样的:

    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,它会阻止它进入。

    【讨论】:

    • 感谢您的回答!但是它对我不起作用。您的解决方案似乎重定向到 localhost/index.php 而不是 localhost/project/index.php 。当然,我可以将其更改为 RewriteRule 。 /project/index.php [L] ,但我不希望将路径硬编码到 .htaccess 文件中
    • 我会的..(对不起,评论完成之前的帖子)
    猜你喜欢
    • 2013-09-19
    • 2018-04-05
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多