当在apache2中使用别名时,配置.htaccess会遇到404错误。
比如以下的alias配置:
Alias /htdocs /home/faund/yiidev/htdocs/wwwroot/
<Directory /home/faund/yiidev/htdocs/wwwroot/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
在/home/faund/yiidev/htdocs/wwwroot/下创建.htaccess内容如下:
<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Don't perform redirects for files and directories that exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For everything else, redirect to index.php:
RewriteRule ^(.*)$ index.php/$1
</ifModule>
结果会出现404错误。
Not Found
The requested URL /home/faund/yiidev/htdocs/wwwroot/index.php/site/login was not found on this server.
此时需要RewriteBase, 后面跟上Alias的web路径,如下面粗体部分:
<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
RewriteBase /htdocs
# Don't perform redirects for files and directories that exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For everything else, redirect to index.php:
RewriteRule ^(.*)$ index.php/$1
</ifModule>
如果此时仍旧出现错误,可以到apache的log中查看原因。如果出现402禁止访问的错误,可以在error_log中查看到这样的错误:
[Sat Apr 12 19:05:37.417862 2014] [rewrite:error] [pid 12639] [client ::1:53474] AH00670: Options FollowSymLinks and SymLinksIfOwn
erMatch are both off, so the RewriteRule directive is also forbidden due to its similar ability to circumvent directory restrictio
ns : /home/faund/yiidev/webapp/larry_book/public_html/site, referer: http://localhost/yiidev/webapp/larry_book/public_html/
[Sat Apr 12 19:05:37.424453 2014] [mpm_prefork:notice] [pid 12620] AH00163: Apache/2.4.6 (Linux/SUSE) OpenSSL/1.0.1e PHP/5.4.20 co
nfigured -- resuming normal operations
这果需要在.htaccess文件中增加“Options +FollowSymlinks”
Options +FollowSymlinks
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteBase /yiidev
RewriteRule ^(.*)$ /yiidev/webapp/larry_book/public_html/index.php/$1
</ifModule>