在OSX上的Users / <username> / Sites目录中的mod_rewrite </username>

时间:2012-03-30 01:36:42

标签: apache codeigniter mod-rewrite

对此可能有一个相当简单的解决方案,但我已经搜索了几天而找不到一个,所以... ...

我正在开发一个OS X盒子上的网站(Lion)。工作站点位于/ Users / username / Sites,我已将该目录添加到/etc/apache2/users/username.conf。我可以毫无问题地查看这些页面。

但是......我正在使用CodeIgniter,我想从URL中删除index.php。对于mod_rewrite来说,这应该是一个相当简单的工作。我已经在目录中添加了一个.htaccess文件(并在上面的conf文件中设置了AllowOverride All)。在谷歌搜索后我发现我需要选项+ FollowSymLinks设置(我在.htaccess文件中做过)。

这个问题是它似乎重写了从localhost / ~username /到/ Users / username / Sites的URL。问题在于,在该形式中,浏览器只是尝试下载index.php文件,而不是执行它。当链接是/Users//Sites/index.php/controller/function时会变得更糟,因为这些文件不存在... CodeIgniter意味着接管index.php,但只有在它被执行时才会接管。 / p>

所以我无法删除Options + FollowSymLinks,因为它会生成Access Forbidden错误,并且由于上述原因我无法将其保留。

有趣的是,将完全相同的网站放在/ Library / WebServer / Documents目录下工作正常。 OS X似乎不介意该目录的FollowSymLinks,可能是因为它被设置为httpd.conf中的DocumentRoot

我的httpd.conf是库存Lion,除了/ Library / Webserver / Documents上的AllowOverride All。 mod_rewrite已启用。

我的username.conf是

<Directory "/Users/username/Sites/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

网站目录中的.htaccess文件是

Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|static|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

显然,我可以开发/ Library / WebServer / Documents,但更愿意在我的本地文件中进行开发。

2 个答案:

答案 0 :(得分:2)

CodeIgniter网站上有一个关于mod rewrite的优秀wiki页面,它涵盖了您对.htaccess文件和CodeIgniter文件本身所做的所有更改。

很容易忘记在配置文件中更改值,例如:

中的index_page
$config['index_page'] = "index.php";

$config['index_page'] = "";

该维基页面上显示的示例.htaccess文件是(我已删除评论):

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

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

如果您发现需要,只需添加AddType和Options属性即可。

答案 1 :(得分:1)

这里有一些技巧可能有用

AddType application/x-httpd-php .php

Options +FollowSymLinks

RewriteEngine   on
RewriteBase  /Users/username/Sites/

RewriteEngine on
RewriteCond $1 !^(index\.php|static|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

您也可以记录重写日志。但建议您在修复所有内容后删除这些行

 RewriteLog "/var/log/httpd/rewrite_log" 
 RewriteLogLevel 9