When I try to setup PHP and Apache in my notebook, I try to enable the .htaccess setting in apache's httpd.conf. After I tried for a few hours (YES, a few hours) at my notebook and google-ing on the internet, finally I found the complete solution.
I record the steps here so that it can help those who want to enable .htaccess and url rewriting in Apache for Windows and also for my own reference in future.
1. Make sure you have setup apache and php correctly. Make sure that you can run php scripts without any problem.
2. Use notepad to open httpd.conf config file. Make use the line "LoadModule rewrite_module modules/mod_rewrite.so
" is un-commented.
3. Under "<directory XXX></directory>
" section, change the line "AllowOverride None
" to "AllowOverride All
".
4. Change the line "AccessFileName .htaccess
" to "AccessFileName htaccess
". This is because in Windows, we can't have a file with filename that starts with a dot.
5. Save the changed file and restart Apache Windows Service. You can then use the file
htaccess
to process your url rewritting.
簡單翻譯一下就是:
1.去掉 LoadModule rewrite_module modules/mod_rewrite.so
前的# (apache 2.2)
2.修改 <directory XXX></directory>
中的 AllowOverride
None 爲
AllowOverride All
3.如果 httpd.conf (在apache的安裝目錄裏conf文件夾) 中 有 AccessFileName .htaccess
這行 就修改爲
AccessFileName htaccess
, 並把網站目錄裏的 .htaccess修改爲 htaccess, 不需要文件後綴,因爲windows不支持以 .開頭的文件。如果httpd.conf裏面有沒有這行,在
<directory XXX></directory>
後面加上 這行 AccessFileName htaccess
就可以了。
4.保存,重啓apache就可以了。然後正確編寫 url rewrite的rule.
一直都在用linux下的apache,剛接觸 win2003下的apache 重寫,網上有很多文章,都提到了前2條,但是對第3條沒有涉及,這一條也很關鍵。以備後用。