macos .htaccess rewriterule 在 mac 上无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7941958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:33:32  来源:igfitidea点击:

.htaccess rewriterule not working correct on mac

phpmacosapache.htaccessmod-rewrite

提问by LostMohican

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 :

我的 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>

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.

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

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

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

Not Found

未找到

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

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

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.

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

my .htaccess file looks like this

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

<ifModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/ [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 : )

令我困扰的是,此配置适用于 Linux 和 Windows,但不适用于 Mac。我觉得注定要失败:)

回答by tong

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

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

But this works:

但这有效:

RewriteEngine On

RewriteBase /~username/YOURPath

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

回答by Aryo

On Mac OSX Mountain Lion, make sure AllowOverrideis set to Allon Directory /

在 Mac OSX Mountain Lion 上,确保AllowOverride设置为Allon 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 FollowSymLinksis included in Options.

还要确保FollowSymLinks包含在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:

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

RewriteBase /~username/site/site/

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

如果您想将.htaccess文件名更改为其他名称,例如.aclhtaccess.txt,请取消对 httpd.conf 中的以下行的注释

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

and change .htaccessin AccessFileNamedirective to anything you like in extra/httpd-default.conffile. For example if you would like to change .htaccessfile to htaccess.txtchange it to:

和改变.htaccessAccessFileName指令任何你像extra/httpd-default.conf文件。例如,如果您想更改.htaccess文件以htaccess.txt将其更改为:

AccessFileName htaccess.txt

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

保存它们,在 OSX Mountain Lion 上重启你的 apache Web 服务器,希望你一切顺利。希望这可以帮助。

回答by Rahul Bharadwaj

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

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

DocumentRoot "/~username/site/"

回答by Yoni Hassin

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

所以 RewriteRule ^Register /register.php

Became RewriteRule ^Register register.php

变成了 RewriteRule ^Register register.php

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

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

回答by renetalk

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

我有同样的问题,但对我来说问题mod_rewrite是没有启用。

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

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

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

After that there was no need to use RewriteBaseetc.

之后就不需要使用RewriteBase等了。

回答by Varun Bhatia

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".

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