.htaccess rewriterule 在 Mac 上无法正常工作

发布于 2024-12-12 19:33:36 字数 850 浏览 0 评论 0原文

我在我的 mac book pro leopard 上有一个网站项目,我使用内置的 apache2 和 php。我已在 httpd.conf 和 user.conf 中进行配置以使用 htaccess。所做的更改如下:

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

<Directory >
    AllowOverride All
</Directory>

问题是当我想打开一个像 localhost/~username/site/site/index.php/welcome,index.php 执行一些操作并找到正确的控制器和正确的页面。

但是当我尝试输入 site/welcome 这样的网站时,apache 给出以下错误:

未找到

在此服务器上找不到请求的 URL Users/username/Sites/site/index.php/welcome。

问题是 apache 得到这个就像它确实是一个文件一样,并且错误语句以文件系统的方式给出它,但是这个请求必须被提取到 index.php 。

我的 .htaccess 文件看起来像这样

<ifModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>

令我困扰的是,此配置在 Linux 和 Windows 上都有效,但在 Mac 上不起作用。我觉得我注定了:)

I have a website project on my mac book pro leopard, and I am using built in apache2 and php. I've made my configurations in both httpd.conf and user.conf to use htaccess. Changes done like :

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

<Directory >
    AllowOverride All
</Directory>

The problem is when i want to open a site like
localhost/~username/site/site/index.php/welcome, index.php does some operations and finds the right controller and the right page.

But when I try to enter site like site/welcome apache gives me the following error :

Not Found

The requested URL Users/username/Sites/site/index.php/welcome was not found on this server.

The problem is apache gets this like it is indeed a file and the error statement gives it in a file system way, but this request must be fetched to index.php instead.

my .htaccess file looks like this

<ifModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>

What bothers me that this configuration works on both Linux and Windows but does not work on Mac. I feel doomed : )

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

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

发布评论

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

评论(6

孤千羽 2024-12-19 19:33:36

我也遇到了同样的问题! “DocumentRoot”在 Lion 中对我不起作用。

但这有效:

RewriteEngine On

RewriteBase /~username/YOURPath

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]

I met the same problem too! 'DocumentRoot' doesn't work for me in Lion.

But this works:

RewriteEngine On

RewriteBase /~username/YOURPath

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
陌上青苔 2024-12-19 19:33:36

上将 AllowOverride 设置为 All

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
</Directory>

在 Mac OSX Mountain Lion 上,确保在 Directory /和文档根路径的根目录

<Directory "/Library/WebServer/Documents">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

:同时确保 FollowSymLinks > 包含在选项中。

如果您的“index.php-less 漂亮 URL”不在您的 DocumentRoot 上,则在您的 .htaccess 文件中添加以下 RewriteBase 目录(其中存在您的 index.php),如下例所示:

RewriteBase /~username/site/site/

如果您想更改 。 htaccess 文件名更改为 .aclhtaccess.txt 等其他内容,取消 httpd.conf 中以下行的注释

# Various default settings
Include /private/etc/apache2/extra/httpd-default.conf

并更改 .htaccess中AccessFileName 指令为 extra/httpd-default.conf 文件中您喜欢的任何内容。例如,如果您想将 .htaccess 文件更改为 htaccess.txt,请将其更改为:

AccessFileName htaccess.txt

保存它们,在 OSX Mountain Lion 上重新启动 apache Web 服务器,并希望您能够很好,可以走了。
希望这有帮助。

On Mac OSX Mountain Lion, make sure AllowOverride is set to All on Directory /

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
</Directory>

and root directory of your document root path:

<Directory "/Library/WebServer/Documents">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

also make sure that FollowSymLinks is included in Options.

if your "index.php-less pretty URL" is not on your DocumentRoot then add the following RewriteBase directory on your .htaccess file where your index.php exists, like the following example:

RewriteBase /~username/site/site/

If you would like to change the .htaccess file name to something else like .acl or htaccess.txt, uncomment the following line on your httpd.conf

# Various default settings
Include /private/etc/apache2/extra/httpd-default.conf

and change .htaccess in AccessFileName directive to anything you like in extra/httpd-default.conf file. For example if you would like to change .htaccess file to htaccess.txt change it to:

AccessFileName htaccess.txt

save them, restart your apache web server on OSX Mountain Lion, and hope you are good to go.
Hope this helps.

或十年 2024-12-19 19:33:36

您需要将以下行添加到 .htaccess 文件中:

DocumentRoot "/~username/site/"

You need to add the following line to your .htaccess file:

DocumentRoot "/~username/site/"
余生一个溪 2024-12-19 19:33:36

我只是遇到了同样的问题,但上面的解决方案对我不起作用,

从一开始删除斜杠就解决了问题(我猜是因为我的浏览器已经在正确的用户下运行)。

所以 RewriteRule ^Register /register.php

变成了 RewriteRule ^Register register.php

这让我发疯!顺便说一句,我正在 mac os 10.8 上运行 xampp (apache 2) 。

I just had the same problem but the solutions above did not work for me,

Removing the slash from the beginning did the trick (i guess because my browser already runs under the correct user).

So RewriteRule ^Register /register.php

Became RewriteRule ^Register register.php

That drove me nuts! BTW I am running xampp (apache 2) on mac os 10.8 .

黄昏下泛黄的笔记 2024-12-19 19:33:36

我遇到了同样的问题,但对我来说问题是 mod_rewrite 未启用。

我必须在 /etc/apache2/httpd.conf 文件中取消注释这两行:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module        libexec/apache2/libphp7.so

之后就不需要使用 RewriteBase 等。

I had the same issue but for me the problem was that mod_rewrite was not enabled.

I had to uncomment these two line in the /etc/apache2/httpd.conf file:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module        libexec/apache2/libphp7.so

After that there was no need to use RewriteBase etc.

池木 2024-12-19 19:33:36

如果我将 .htaccess 放入 /Library/WebServer/Documents 并打开“localhost/”;对其进行测试,这按预期工作。它只是在“~/Sites”中不起作用。

If I place a .htaccess into /Library/WebServer/Documents and open "localhost/"; to test it, this works as expected. It just doesn't work in "~/Sites".

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