如何在OS X上使用mod_rewrite

时间:2012-09-25 18:15:56

标签: php macos apache .htaccess mod-rewrite

我使用mac os x 10.6.8并且我遇到了mod_rewrite的问题 - 它非常适合。

我在/etc/apache2/users/my_user.conf/etc/apache2/httpd.conf中更改了 AllowOverride All

.htaccess代码:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

PHP:

var_dump($_GET['q']);

结果:

Trying: http://localhost/~nonamez123/konkurs/asgfsddsh  

Not Found

The requested URL /Users/nonamez123/Sites/konkurs/index.php was not found on this server.

如您所见,重写index.php正在进行,但无法找到index.php

日志:

127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (3) [perdir
/Users/nonamez123/Sites/konkurs/] strip per-dir prefix:
/Users/nonamez123/Sites/konkurs/asfdasdf -    asfdasdf
127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (3) [perdir
/Users/nonamez123/Sites/konkurs/] applying pattern '^(.*)$' to uri
'asfdasdf'
127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (4) [perdir
/Users/nonamez123/Sites/konkurs/] RewriteCond:
input='/Users/nonamez123/Sites/konkurs/asfdasdf' pattern='!-f' =>
matched
127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (4) [perdir
/Users/nonamez123/Sites/konkurs/] RewriteCond:
input='/Users/nonamez123/Sites/konkurs/asfdasdf' pattern='!-d' =>
matched
127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (2) [perdir
/Users/nonamez123/Sites/konkurs/] rewrite 'asfdasdf' ->
'index.php?q=asfdasdf'
127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (3) split
uri=index.php?q=asfdasdf -    uri=index.php, args=q=asfdasdf
127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (3) [perdir
/Users/nonamez123/Sites/konkurs/] add per-dir prefix: index.php ->
/Users/nonamez123/Sites/konkurs/index.php
127.0.0.1 - - [25/Sep/2012:21:06:13 +0300] [localhost/sid#10080d720][rid#1009d64a8/initial] (1) [perdir
/Users/nonamez123/Sites/konkurs/] internal redirect with
/Users/nonamez123/Sites/konkurs/index.php [INTERNAL REDIRECT]

1 个答案:

答案 0 :(得分:0)

我将把它们放在〜/ Library / WebServer / Documents中的webserver根目录中,而不是将你的开发文件放在~user / Sites中。我有更好的运气。

然后,确保/etc/apache2/httpd.conf中的AllowOverride All位于标记为<Directory "/Library/WebServer/Documents">的目录标记内。在httpd.conf中有几个AllowOverride指令,如果你使用我的方法,你要修改的指令就在217行左右。

还要确保取消注释行LoadModule rewrite_module libexec/apache2/mod_rewrite.so

最后确保使用sudo apachectl graceful重新启动apache。

如果您确实想要使用您的用户站点目录,则此博客条目有一些提示。 http://johnattebury.com/blog/2012/07/upgrading-to-mountain-lion-forbidden-403-you-dont-have-permission-to-access-username-on-this-server/