在 Mac OS X Lion (10.7) 上重写 Apache2 中的相对路径

发布于 2024-12-16 16:06:40 字数 1012 浏览 4 评论 0原文

我已经彻底搜索了该网站并用谷歌搜索了此内容,但无济于事。 我在 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 技术交流群。

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

发布评论

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

评论(1

妄司 2024-12-23 16:06:40

问题来自您的虚拟主机配置:

Not Found
The requested URL /Users/milad/Sites/newave/index.php was not found on this server.

意思很明确:“您网站的基础是 /Users/milad/Sites/newave/”。
因此,如果您希望网站的基础为 /~milad/mysite/ ,请尝试更改 DocumentRoot

<VirtualHost *>
    ServerName mysite.com
    DocumentRoot "/~milad/mysite/"

</VirtualHost>

当然,您网站的所有文件都必须位于该文件夹中“/~milad/mysite/”。

然后在你的 htaccess 文件中(注意 ^(.*?)$ 相当于 (.*) ,我不明白为什么您需要 RewriteRule ^$ 因为它应该在最后正确处理):

Options +FollowSymLinks
Options +Indexes

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?:url=$1 [L,QSA]

请告诉我它是否有效


编辑:根据评论,这是一个适合我的虚拟主机文件:

<VirtualHost *>
    DocumentRoot "/web/htdocs/olivier/wwog"
    ServerName wwog.fr
    ServerAlias *.wwog.fr

    ErrorLog "/web/logs/wwog.error.log"
    CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/wwog/access.%Y-%m-%d-%H_%M_%S.log 5M" combined

    DirectoryIndex index.php index.htm

    <Location />
        # Compression:
        # (http://httpd.apache.org/docs/2.0/mod/mod_deflate.html)
        # Insert filter
        AddOutputFilterByType DEFLATE text/html text/plain
        SetOutputFilter DEFLATE

        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # Don't compress images
        SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary

        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </Location>

    # Add "Cache-control: public" = valid for 480 weeks
    # for proxies to keep images in cache:
    <FilesMatch "\.(ico|flv|jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "max-age=290304000, public"
    </FilesMatch>

    # Forbid files that start with "_"
    <FilesMatch "^_">
        Order allow,deny
        Deny from all
        Satisfy all 
    </FilesMatch>

    # Forbid .htaccess and .htpasswd
    <FilesMatch "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy all 
    </FilesMatch>

    <Directory "/web/htdocs/wwog">
        Order allow,deny
        Allow from all
        Deny from none
        AllowOverride All 
    </Directory>

</VirtualHost>

The problem comes from your vhost configuration:

Not Found
The requested URL /Users/milad/Sites/newave/index.php was not found on this server.

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 DocumentRoot

<VirtualHost *>
    ServerName mysite.com
    DocumentRoot "/~milad/mysite/"

</VirtualHost>

Of 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 the RewriteRule ^$ because it should be handled properly at the end):

Options +FollowSymLinks
Options +Indexes

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?:url=$1 [L,QSA]

Please tell me if it works


Edit: Following the comments, here's a vhost file that works for me:

<VirtualHost *>
    DocumentRoot "/web/htdocs/olivier/wwog"
    ServerName wwog.fr
    ServerAlias *.wwog.fr

    ErrorLog "/web/logs/wwog.error.log"
    CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/wwog/access.%Y-%m-%d-%H_%M_%S.log 5M" combined

    DirectoryIndex index.php index.htm

    <Location />
        # Compression:
        # (http://httpd.apache.org/docs/2.0/mod/mod_deflate.html)
        # Insert filter
        AddOutputFilterByType DEFLATE text/html text/plain
        SetOutputFilter DEFLATE

        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # Don't compress images
        SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary

        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </Location>

    # Add "Cache-control: public" = valid for 480 weeks
    # for proxies to keep images in cache:
    <FilesMatch "\.(ico|flv|jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "max-age=290304000, public"
    </FilesMatch>

    # Forbid files that start with "_"
    <FilesMatch "^_">
        Order allow,deny
        Deny from all
        Satisfy all 
    </FilesMatch>

    # Forbid .htaccess and .htpasswd
    <FilesMatch "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy all 
    </FilesMatch>

    <Directory "/web/htdocs/wwog">
        Order allow,deny
        Allow from all
        Deny from none
        AllowOverride All 
    </Directory>

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