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
.htaccess rewriterule not working correct on mac
提问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 AllowOverride
is set to All
on Directory /
在 Mac OSX Mountain Lion 上,确保AllowOverride
设置为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
.
还要确保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 .htaccess
file name to something else like .acl
or htaccess.txt
, uncomment the following line on your httpd.conf
如果您想将.htaccess
文件名更改为其他名称,例如.acl
或htaccess.txt
,请取消对 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:
和改变.htaccess
在AccessFileName
指令任何你像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_rewrite
was 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 RewriteBase
etc.
之后就不需要使用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”中不起作用。