前言
今天將手頭的ubuntu12.04 upgrade to ubuntu14.04,發現apache2.2 也自動升級到了apache2.4,然後之前的一個website的rewrite規則就不好用了。於是,從官網上找了點資料,然後自己反覆測試,終於再次run起來了,並對rewrite中需要注意的細節有了一些心得。
新語法
在Apache2.4中,以前的Order 和Allow Deny指令已經取消了,改成了Require。Require的語法最常用的方式就是
Require all granted/denied
<RequireAll>
Require all granted
Require not ip 192.168.2.*
Require not host example.com
</RequreAll>
RequireAll從名字上可以理解,就是需要裏面的所有的auth都要滿足,和以前的Satisfy用法類似
<RequireAny>
RequireAny就是只要有任何一個RequireAny中包含的auth項滿足,就滿足。
<RequireNone>
RequireNone就是需要包含在RequireNone中任何一個項都不滿足才通過。
錯誤解決
說明:最開是我升級後,重新配置了website的監聽端口以及DocumentRoot,然後訪問localhost:port發現直接給出
Forbidden
You don't have permission to access /xxx.html on this server.
因爲我的website之前就是在當前用戶的/home/user/目錄,權限一般是700是不讓其他用戶(apache內置默認的Daemon用戶)訪問的,於是在/etc/apache2/apache.conf文件中找到設置${APACHE_RUN_USER}和${APACHE_RUN_GROUP}的地方,設置User 當前用戶 ,Group 當前用戶,restart Apache,本以爲就此完事,發現仍然提示之前的Forbiden錯誤。
反覆思考是不是路徑不對,但仔細覈對發現並沒有錯。後面在網上google了一番,找到有人問同樣的問題,上面提示說是根目錄默認是denied的,於是gvim打開/etc/apache2/apache2.conf仔細覈查,發現如下配置信息:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
原來如此,確實apache默認設置了根目錄是不可訪問的,但是我不是設置了自己的website的DocumentRoot目錄是可訪問的嗎??
這個疑問困擾了半天,直到突然發現在sites-enabled/000-default.conf中才發現
上面的apache2.conf配置的是/var/www目錄,而000-default.conf裏面配置的是/var/www/html目錄,難道是說配置某個目錄只代表該目錄下面的文件可以訪問??
於是在自己的website對應的VirtualHost裏面增加
<Directory /home/xxx/workspace/php_web/abc>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">/home/xxx/workspace/php_web/abc/website</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">></span>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^([^./]+)$
RewriteRule ^.*$ index.php/%1 [L]
RewriteCond %{REQUEST_URI} ^([^./]+)/([^./]+)$
RewriteRule ^.*$ index.php/%1/%2 [L]
DirectoryIndex index.php index.html
</Directory>
重啓apache,然後訪問,成功打開了主頁。其實就是說如果要想讓你的website可以訪問根目錄,必須要在它的父級目錄或者祖先目錄設置allow
高高興興,準備收工,隨手點兩個登陸發現,登陸失敗。檢查數據庫沒有問題。並且提示的是路徑沒有找到,直接推斷應該是重寫出了問題。看了幾遍上面的重寫規則配置,大體上沒有問題,直覺感覺有可能是REQUEST_URI忽略了/導致的問題,於是加上去變成
<Directory /home/xxx/workspace/php_web/abc/website>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([^./]+)$
RewriteRule ^.*$ index.php/%1 [L]
RewriteCond %{REQUEST_URI} ^/([^./]+)/([^./]+)$
RewriteRule ^.*$ index.php/%1/%2 [L]
DirectoryIndex index.php index.html
</Directory>
重啓Apache,OK!
總結
一、apache access設置必須要設置website的父級目錄或者祖先目錄才能使DocumentRoot目錄被直接訪問
二、REQUEST_URI 包括了路徑最開始的/