在 Mac OS X Lion (10.7) 上重写 Apache2 中的相对路径
我已经彻底搜索了该网站并用谷歌搜索了此内容,但无济于事。 我在 Mac OS X 上使用 Apache2 + PHP。 我没有对其中任何一个进行太多的配置更改,足以让一切正常工作。 这是我的 .htaccess 文件:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteBase /~milad/mysite/
RewriteEngine On
RewriteRule ^$ index.php?:url [L,QSA] #Handling tail with no parameters
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)$ index.php?:url=$1 [L,QSA]
</IfModule>
这适用于位于 http://localhost/~milad/mysite/* 的所有文件。 但我想让我的 .htaccess
文件独立于我的安装细节,即,我不想在我的代码中包含 RewriteBase ...
行。
但是当我删除该行时,Apache 尝试将其映射到我不理解的 URL。 我得到的是:
未找到
在此服务器上找不到请求的 URL /Users/milad/Sites/newave/index.php。
这太荒谬了,因为 index.php
文件正是该 URI 所指向的位置。 无论如何,当我尝试重写为 /index.php
而不是 index.php
时,我发现它被重写为 http://localhost/index .php
。那么为什么我不能只使用相对路径,或者只使用 ./index.php
(是的,我也尝试过)。
任何见解将不胜感激。
I've searched the site thoroughly and Googled for this as well, but to no avail.
I use Apache2 + PHP on my Mac OS X.
I haven't changed much of the configuration on any of them, just enough to get everything working correctly.
Here's my .htaccess
file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteBase /~milad/mysite/
RewriteEngine On
RewriteRule ^$ index.php?:url [L,QSA] #Handling tail with no parameters
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)$ index.php?:url=$1 [L,QSA]
</IfModule>
This works just fine for all the files located at http://localhost/~milad/mysite/*
.
But I want to make my .htaccess
file independent of my installation's particulars, i.e., I don't want to have to include the RewriteBase ...
line in my code.
But when I remove that line, Apache tries to map it to a URL which I don't understand.
What I get is:
Not Found
The requested URL /Users/milad/Sites/newave/index.php was not found on this server.
which is just ridiculous, because the index.php
file is JUST where that URI is pointing to.
Anyway, when I try to rewrite to /index.php
instead of index.php
, I find that it is being rewritten to http://localhost/index.php
. So how is it that I can't use just the relative path, or just use ./index.php
(Yes, I've tried that, too).
Any insight would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题来自您的虚拟主机配置:
意思很明确:“您网站的基础是
/Users/milad/Sites/newave/
”。因此,如果您希望网站的基础为
/~milad/mysite/
,请尝试更改 DocumentRoot当然,您网站的所有文件都必须位于该文件夹中“
/~milad/mysite/
”。然后在你的 htaccess 文件中(注意
^(.*?)$
相当于(.*)
,我不明白为什么您需要RewriteRule ^$
因为它应该在最后正确处理):请告诉我它是否有效
编辑:根据评论,这是一个适合我的虚拟主机文件:
The problem comes from your vhost configuration:
Means clearly: "the base of your site is
/Users/milad/Sites/newave/
".So if you want the base for you site to be
/~milad/mysite/
try to change the DocumentRootOf course, all the files of you site must be in the folder "
/~milad/mysite/
".Then in your htaccess file (notice that
^(.*?)$
is equivalent to(.*)
and I don't understand why you need theRewriteRule ^$
because it should be handled properly at the end):Please tell me if it works
Edit: Following the comments, here's a vhost file that works for me: