httpd.conf 中手動編輯虛擬主機區塊。
# APMServ 默認虛擬主機
NameVirtualHost *:80
<VirtualHost *:80>
ServerName *
DocumentRoot "D:/APMServ5.2.6/www/htdocs"
RewriteEngine On
RewriteRule ^/$ /phpinfo.php [R=301]
<Directory "D:/APMServ5.2.6/www/htdocs">
Options FollowSymLinks IncludesNOEXEC Indexes
DirectoryIndex index.html index.htm default.htm index.php default.php index.cgi default.cgi index.pl default.pl index.shtml
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>
| 指令 | 作用 |
|---|---|
RewriteEngine On | 啟用重寫引擎 |
RewriteRule ^/$ /phpinfo.php [R=301] | 匹配根路徑 /,301 永久重定向到 /phpinfo.php |
[R=301] | HTTP 301 狀態碼,利於 SEO 權重傳遞 |
# 隱藏 index.php (ThinkPHP/CodeIgniter 風格)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# 強制 HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 防盜鏈
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F,NC]
修改後重啟 Apache 生效。建議在 <Directory> 中設置 AllowOverride All 並改用 .htaccess 管理項目級規則,避免頻繁編輯主配置。